<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java👉Swing组件 | 小李博客</title><meta name="keywords" content="JavaSE"><meta name="author" content="小李博客"><meta name="copyright" content="小李博客"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Swing程序设计实际开发中使用WindowBuilder插件进行图形界面设计，更加方便快捷。WindowBuilder插件下载WindowBuilder插件安装WindowBuilder插件使用GUI(图形用户界面)为程序提供图形界面，Swing是AWT组件的增强组件，被称为“轻量级组件”，完全由java语言编写，可以在任何平台上运行，相反，依赖于本地平台的组件被称为“重量级组件”，如AWT。S">
<meta property="og:type" content="article">
<meta property="og:title" content="Java👉Swing组件">
<meta property="og:url" content="http://xiaoliblog.cn/page/java04.html">
<meta property="og:site_name" content="小李博客">
<meta property="og:description" content="Swing程序设计实际开发中使用WindowBuilder插件进行图形界面设计，更加方便快捷。WindowBuilder插件下载WindowBuilder插件安装WindowBuilder插件使用GUI(图形用户界面)为程序提供图形界面，Swing是AWT组件的增强组件，被称为“轻量级组件”，完全由java语言编写，可以在任何平台上运行，相反，依赖于本地平台的组件被称为“重量级组件”，如AWT。S">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png">
<meta property="article:published_time" content="2021-01-05T09:57:46.884Z">
<meta property="article:modified_time" content="2021-03-20T10:10:40.675Z">
<meta property="article:author" content="小李博客">
<meta property="article:tag" content="JavaSE">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png"><link rel="shortcut icon" href="https://cdn.jsdelivr.net/gh/xiaoliblog/image@6b5e7ef72be1c8973d94e5a9c49accbf775ad820/2021/02/01/c485da031fe0e464d04eaba8a66c4a8f.png"><link rel="canonical" href="http://xiaoliblog.cn/page/java04"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/instantsearch.js@2.10.5/dist/instantsearch.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/instantsearch.js@2.10.5/dist/instantsearch.min.js" defer></script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: {"appId":"RTG4CPNBLJ","apiKey":"f1745bdad68ceec57653b78244fe332c","indexName":"MyBlogIndex","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":100,"languages":{"author":"作者: 小李博客","link":"链接: ","source":"来源: 小李博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'mediumZoom',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-03-20 18:10:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><link rel="stylesheet" href="/css/MyStyle/MyStyle.css" media="defer" onload="this.media='all'"/><link rel="stylesheet" href="/css/MyStyle/tagStyle.css" media="defer" onload="this.media='all'"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zykjofficial/zykjresource@master/css/font-awesome-animation.min.css" media="defer" onload="this.media='all'"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/plugins.min.css" media="defer" onload="this.media='all'"><meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/atom.xml" title="小李博客" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@6b5e7ef72be1c8973d94e5a9c49accbf775ad820/2021/02/01/c485da031fe0e464d04eaba8a66c4a8f.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">210</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">38</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">56</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/box/"><i class="fa-fw fa fa-briefcase"></i><span> 工具箱</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-book"></i><span> 找文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-address-card"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小李博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/box/"><i class="fa-fw fa fa-briefcase"></i><span> 工具箱</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-book"></i><span> 找文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-address-card"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">Java👉Swing组件</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-01-05T09:57:46.884Z" title="发表于 2021-01-05 17:57:46">2021-01-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-03-20T10:10:40.675Z" title="更新于 2021-03-20 18:10:40">2021-03-20</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java%E5%90%8E%E7%AB%AF/">Java后端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java%E5%90%8E%E7%AB%AF/JavaSE/">JavaSE</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">10.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>49分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java👉Swing组件"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div><article class="post-content" id="article-container"><h1 id="Swing程序设计"><a href="#Swing程序设计" class="headerlink" title="Swing程序设计"></a>Swing程序设计</h1><p>实际开发中使用WindowBuilder插件进行图形界面设计，更加方便快捷。<br><a target="_blank" rel="noopener" href="https://www.eclipse.org/windowbuilder/download.php">WindowBuilder插件下载</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/chilongxiaba/article/details/106314810">WindowBuilder插件安装</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_34491508/article/details/80113911">WindowBuilder插件使用</a><br><code>GUI(图形用户界面)</code>为程序提供图形界面，<code>Swing</code>是AWT组件的增强组件，被称为“<code>轻量级组件</code>”，完全由java语言编写，可以在任何平台上运行，相反，依赖于本地平台的组件被称为“<code>重量级组件</code>”，如<code>AWT</code>。<br>Swng包的层次结构和继承关系：<br><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/51007f38a006bb3bde43c7d9cf60cd39.png"></p>
<h2 id="常用Swing组件"><a href="#常用Swing组件" class="headerlink" title="常用Swing组件"></a>常用Swing组件</h2><p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/d2ab88097bc3f749bf007b3a2b19d39c.png"></p>
<h2 id="常用窗体"><a href="#常用窗体" class="headerlink" title="常用窗体"></a>常用窗体</h2><p><code>JFrame窗体</code>是一个<code>容器</code>，是各个组件的载体，可以通过继承<code>java.swing.JFrame</code>类创建一个窗口，在窗口中添加组件。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造方法</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JFrame</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JFrame</span><span class="params">(String title)</span></span>;</span><br><span class="line"><span class="comment">//语法格式</span></span><br><span class="line">JFrame f=<span class="keyword">new</span> JFrame(title);  <span class="comment">//f为JFrame类的对象</span></span><br><span class="line">Container c=f.getContentPane(); <span class="comment">//c为Container类的对象，可以使用JFrame对象调用getContentPane()方法获取。</span></span><br><span class="line"><span class="comment">//调用getContentPane()方法将窗体转为容器，然后在容器里添加组件。</span></span><br><span class="line">c.add(<span class="keyword">new</span> JButton(<span class="string">&quot;按钮&quot;</span>));</span><br><span class="line"><span class="comment">//remove()方法将组件从容器里删除</span></span><br><span class="line">c.remove(<span class="keyword">new</span> JButton(<span class="string">&quot;按钮&quot;</span>));</span><br></pre></td></tr></table></figure>
<p><code>继承JFrame实现:</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Blog_02;  </span><br><span class="line"><span class="keyword">import</span> java.awt.*;    <span class="comment">//导入awt包</span></span><br><span class="line"><span class="keyword">import</span> javax.swing.*; <span class="comment">//导入swing包</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">blog_02_05</span> <span class="keyword">extends</span> <span class="title">JFrame</span></span>&#123;  <span class="comment">//定义一个类继承JFrame</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">CreateJFrame</span><span class="params">(String title)</span> </span>&#123; <span class="comment">//定义一个方法创建JFrame</span></span><br><span class="line">		JFrame f=<span class="keyword">new</span> JFrame(title);    <span class="comment">//实例化一个JFrame对象，并设置窗口名</span></span><br><span class="line">		Container c=f.getContentPane();  <span class="comment">//获取容器</span></span><br><span class="line">		JLabel l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;这是一个JFrame窗体&quot;</span>); <span class="comment">//标签</span></span><br><span class="line">		<span class="comment">//使标签上的文字居中</span></span><br><span class="line">		l1.setHorizontalAlignment(SwingConstants.CENTER);</span><br><span class="line">		c.add(l1);</span><br><span class="line">		c.setBackground(Color.white);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		f.setSize(<span class="number">400</span>,<span class="number">250</span>); <span class="comment">//setSize(int x,int y)</span></span><br><span class="line">		<span class="comment">//设置窗体关闭方式</span></span><br><span class="line">		f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);</span><br><span class="line">		<span class="comment">//设置窗口相对于指定组件的位置。 如果组件当前未显示或者 c 为 null，则此窗口将置于屏幕的中央。</span></span><br><span class="line">		f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">new</span> blog_02_05().CreateJFrame(<span class="string">&quot;创建一个JFrame窗体&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/7a0309cfe31e440dcf68c2a567123149.png"><br>关于窗口关闭方式:<br>1.<code>DO_NOTHING</code>_ON_CLOSE：什么都不做就将窗口关闭。<br>2.<code>DISPOSE</code>_ON_CLOSE：代表任何注册监听程序对象后会自动隐藏并释放窗体。<br>3.<code>HIDE</code>_ON_CLOSE：表示隐藏窗口的默认窗口关闭。<br>4.<code>EXIT</code>_ON_CLOSE：退出应用程序默认窗口关闭。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">1.导入包:</span></span><br><span class="line"><span class="comment">import java.awt.*;</span></span><br><span class="line"><span class="comment">import javax.swing;</span></span><br><span class="line"><span class="comment">2.定义类，声明组件。</span></span><br><span class="line"><span class="comment">3.构造方法中new所有组件。</span></span><br><span class="line"><span class="comment">4.主方法main()中，new 构造方法。</span></span><br><span class="line"><span class="comment">5,JFrame为容器，JPanel为面板，组件加到面板，面板再加到容器上。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">package</span> Blog_02;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 三个JPanel分别对应三行，即用户名、密码、按钮三行，任何网格布局3行1列布局</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">blog_02_06</span> </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JLabel l1,l2;</span><br><span class="line">	JPanel p1,p2,p3;</span><br><span class="line">	JButton b1,b2;</span><br><span class="line">	JTextField t;</span><br><span class="line">	JPasswordField pw;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">blog_02_06</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;用户登录界面&quot;</span>);</span><br><span class="line">		l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;用户名&quot;</span>);</span><br><span class="line">		l2=<span class="keyword">new</span> JLabel(<span class="string">&quot;密  码&quot;</span>);</span><br><span class="line">		b1=<span class="keyword">new</span> JButton(<span class="string">&quot;登录&quot;</span>);</span><br><span class="line">		b2=<span class="keyword">new</span> JButton(<span class="string">&quot;注册&quot;</span>);</span><br><span class="line">		t=<span class="keyword">new</span> JTextField(<span class="number">10</span>);</span><br><span class="line">		pw=<span class="keyword">new</span> JPasswordField(<span class="number">10</span>);</span><br><span class="line">		p1=<span class="keyword">new</span> JPanel();</span><br><span class="line">		p2=<span class="keyword">new</span> JPanel();</span><br><span class="line">		p3=<span class="keyword">new</span> JPanel();</span><br><span class="line">		<span class="comment">//设置按键文本颜色</span></span><br><span class="line">		b1.setForeground(Color.BLUE);</span><br><span class="line">		b2.setForeground(Color.red);</span><br><span class="line">		b1.setBackground(Color.gray);</span><br><span class="line">		b2.setBackground(Color.black);</span><br><span class="line">		<span class="comment">//第一行</span></span><br><span class="line">		p1.add(l1);</span><br><span class="line">		p1.add(t);</span><br><span class="line">		p1.setLayout(<span class="keyword">new</span> FlowLayout());</span><br><span class="line">		<span class="comment">//第二行</span></span><br><span class="line">		p2.add(l2);</span><br><span class="line">		p2.add(pw);</span><br><span class="line">		p2.setLayout(<span class="keyword">new</span> FlowLayout());</span><br><span class="line">		<span class="comment">//第三行</span></span><br><span class="line">		p3.add(b1);</span><br><span class="line">		p3.add(b2);</span><br><span class="line">		p3.setLayout(<span class="keyword">new</span> FlowLayout());</span><br><span class="line">		<span class="comment">//加到容器</span></span><br><span class="line">		f.add(p1);</span><br><span class="line">		f.add(p2);</span><br><span class="line">		f.add(p3);</span><br><span class="line">		f.setLayout(<span class="keyword">new</span> GridLayout(<span class="number">3</span>,<span class="number">1</span>));<span class="comment">//设置网格布局</span></span><br><span class="line">		f.setSize(<span class="number">230</span>,<span class="number">150</span>);</span><br><span class="line">		f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setResizable(<span class="keyword">false</span>);<span class="comment">//窗口大小是否可变</span></span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="keyword">new</span> blog_02_06();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/9ee3ba3cd8b946c7cc1e2bba3680195e.png"></p>
<hr>
<p>JDialog窗体是Swing组件中的对话框，它继承了AWT组件中的<code>java.awt.Dialog</code>类。<br>JDialog窗体的功能是<code>从一个窗体中弹出另一个窗体</code>。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;构造方法</span><br><span class="line">public JDialog();  &#x2F;&#x2F;没有标题和父窗口的对话框</span><br><span class="line">public JDialog(Frame f); &#x2F;&#x2F;创建一个指定父窗口的对话框</span><br><span class="line">public JDialog(Frame f,boolean model); &#x2F;&#x2F;创建一个指定类型的对话框，并指定父窗口</span><br><span class="line">public JDialog(Frame f,String title);  &#x2F;&#x2F;指定标题和父窗口</span><br><span class="line">public JDialog(Frame f,String title,boolean model); &#x2F;&#x2F;指定标题、窗体、模式的对话框。</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Blog_02;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyJDialog</span> <span class="keyword">extends</span> <span class="title">JDialog</span></span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">MyJDialog</span><span class="params">(MyFrame frame)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//实例化一个JDialog类对象，指定对话框的父窗体、标题和类型</span></span><br><span class="line">		<span class="keyword">super</span>(frame,<span class="string">&quot;第一个JDialog窗体&quot;</span>,<span class="keyword">true</span>);</span><br><span class="line">		Container c=getContentPane(); <span class="comment">//创建一个容器</span></span><br><span class="line">		c.add(<span class="keyword">new</span> JLabel(<span class="string">&quot;这是一个对话框&quot;</span>));</span><br><span class="line">		setBounds(<span class="number">120</span>,<span class="number">120</span>,<span class="number">100</span>,<span class="number">100</span>); <span class="comment">//设置对话框大小</span></span><br><span class="line">		setLocationRelativeTo(<span class="keyword">null</span>); <span class="comment">//居中</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFrame</span> <span class="keyword">extends</span> <span class="title">JFrame</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">MyFrame</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Container c=getContentPane(); <span class="comment">//创建容器</span></span><br><span class="line">		c.setLayout(<span class="keyword">null</span>);</span><br><span class="line">		JLabel l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;这是一个JFrame窗体&quot;</span>);</span><br><span class="line">		<span class="comment">//标签居中</span></span><br><span class="line">		l1.setHorizontalAlignment(SwingConstants.CENTER);</span><br><span class="line">		c.add(l1);</span><br><span class="line">		JButton b=<span class="keyword">new</span> JButton(<span class="string">&quot;弹出对话框&quot;</span>);</span><br><span class="line">		b.setBounds(<span class="number">10</span>,<span class="number">10</span>,<span class="number">100</span>,<span class="number">21</span>);</span><br><span class="line">		b.addActionListener(<span class="keyword">new</span> ActionListener() &#123;</span><br><span class="line">			<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">				<span class="keyword">new</span> MyJDialog(MyFrame.<span class="keyword">this</span>).setVisible(<span class="keyword">true</span>);</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">		c.add(b);</span><br><span class="line">		setSize(<span class="number">200</span>,<span class="number">200</span>);</span><br><span class="line">		setVisible(<span class="keyword">true</span>);</span><br><span class="line">		setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="keyword">new</span> MyFrame();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/4a41eaf54986fd823f9fa08a63af06a0.png"></p>
<h2 id="组件的使用"><a href="#组件的使用" class="headerlink" title="组件的使用"></a>组件的使用</h2><h3 id="JLabel标签"><a href="#JLabel标签" class="headerlink" title="JLabel标签"></a>JLabel标签</h3><p>标签由JLabel类定义，它的父类为JComponent类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造方法</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JLabel</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JLabel</span><span class="params">(Icon icon)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JLabel</span><span class="params">(Icon icon,<span class="keyword">int</span> aligment)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JLabel</span><span class="params">(String text,<span class="keyword">int</span> aligment)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">JLabel</span><span class="params">(String text,Icon icon,<span class="keyword">int</span> aligment)</span></span>; <span class="comment">//带文本，带图标的JLabel对象，并设置标签内容的水平对齐方式。</span></span><br></pre></td></tr></table></figure>
<h3 id="图标的使用"><a href="#图标的使用" class="headerlink" title="图标的使用"></a>图标的使用</h3><p>Swing中图标可以放置在按钮、标签等组件上。<br>``java<br>ImageIcon img=new ImageIcon(“图片URL”);<br>JButton b=new JButton(img);</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">### 文本组件</span><br><span class="line">​&#96;&#96;&#96;java</span><br><span class="line">JTextField t&#x3D;new JTextField(10); &#x2F;&#x2F;长度</span><br><span class="line">JTextArea a&#x3D;new JTextArea(5,5);&#x2F;&#x2F;5行5列</span><br><span class="line">JPasswordField &#x3D;p&#x3D;new JPasswordField();</span><br><span class="line">p.setEchoChar(&quot;%&quot;);&#x2F;&#x2F;设置输入密码显示的字符</span><br></pre></td></tr></table></figure>
<h3 id="按钮组件"><a href="#按钮组件" class="headerlink" title="按钮组件"></a>按钮组件</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">JFrame f=<span class="keyword">new</span> JFrame();</span><br><span class="line">JPanel p=<span class="keyword">new</span> JPanel();</span><br><span class="line"><span class="comment">//1.创建按钮组对象；</span></span><br><span class="line">ButtonGroup bg=<span class="keyword">new</span> ButtonGroup();</span><br><span class="line"><span class="comment">//2.创建若干单选按钮对象</span></span><br><span class="line">JRadioButton b1=<span class="keyword">new</span> JRadoButton(<span class="string">&quot;男&quot;</span>);</span><br><span class="line">JRadioButton b1=<span class="keyword">new</span> JRadoButton(<span class="string">&quot;女&quot;</span>);</span><br><span class="line"><span class="comment">//3.将单选按钮加到按钮组中</span></span><br><span class="line">bg.add(b1);</span><br><span class="line">bg.add(b2);</span><br><span class="line"><span class="comment">//4.将单选按钮加到容器中。</span></span><br><span class="line">p.add(b1);</span><br><span class="line">p.add(b2);</span><br><span class="line">f.add(p);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Windos;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 单选和复选按钮 </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JLabel l,l1;</span><br><span class="line">	JButton b1,b2;</span><br><span class="line">	JPanel p1,p2,p3;</span><br><span class="line">	JCheckBox fx1,fx2,fx3; <span class="comment">//复选按钮</span></span><br><span class="line">	</span><br><span class="line">	JRadioButton dx1,dx2;  <span class="comment">//单选按钮</span></span><br><span class="line">	</span><br><span class="line">	ButtonGroup bg;<span class="comment">//把单选按钮放进一个组里,默认功能是组里的按钮只能选一个</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">public</span> 单选和复选按钮() &#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame();</span><br><span class="line">		p1=<span class="keyword">new</span> JPanel();</span><br><span class="line">		p2=<span class="keyword">new</span> JPanel();</span><br><span class="line">		p3=<span class="keyword">new</span> JPanel();</span><br><span class="line">		l=<span class="keyword">new</span> JLabel(<span class="string">&quot;特长&quot;</span>);</span><br><span class="line">		l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;性别&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		b1=<span class="keyword">new</span> JButton(<span class="string">&quot;注册&quot;</span>);</span><br><span class="line">		b2=<span class="keyword">new</span> JButton(<span class="string">&quot;取消&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		fx1=<span class="keyword">new</span> JCheckBox(<span class="string">&quot;音乐&quot;</span>);</span><br><span class="line">		fx2=<span class="keyword">new</span> JCheckBox(<span class="string">&quot;体育&quot;</span>);</span><br><span class="line">		fx3=<span class="keyword">new</span> JCheckBox(<span class="string">&quot;文艺&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		dx1=<span class="keyword">new</span> JRadioButton(<span class="string">&quot;男&quot;</span>);</span><br><span class="line">		dx2=<span class="keyword">new</span> JRadioButton(<span class="string">&quot;女&quot;</span>);</span><br><span class="line">		<span class="comment">//按钮组 只能选一个勾选 必须填</span></span><br><span class="line">		bg=<span class="keyword">new</span> ButtonGroup();</span><br><span class="line">		bg.add(dx1);</span><br><span class="line">		bg.add(dx2);</span><br><span class="line">		<span class="comment">//</span></span><br><span class="line">		p1.add(l);</span><br><span class="line">		p1.add(fx1);</span><br><span class="line">		p1.add(fx2);</span><br><span class="line">		p1.add(fx3);</span><br><span class="line">		</span><br><span class="line">		p2.add(l1);</span><br><span class="line">		<span class="comment">//还是一个一个加不能添加组</span></span><br><span class="line">		p2.add(dx1);</span><br><span class="line">		p2.add(dx2);</span><br><span class="line">		</span><br><span class="line">		p3.add(b1);</span><br><span class="line">		p3.add(b2);</span><br><span class="line">		</span><br><span class="line">		f.add(p1);</span><br><span class="line">		f.add(p2);</span><br><span class="line">		f.add(p3);</span><br><span class="line">		<span class="comment">//其余三个面板为流式布局</span></span><br><span class="line">		f.setLayout(<span class="keyword">new</span> GridLayout(<span class="number">3</span>,<span class="number">1</span>)); <span class="comment">//网格布局</span></span><br><span class="line">		f.setSize(<span class="number">240</span>,<span class="number">150</span>);</span><br><span class="line">		f.setLocation(<span class="number">300</span>,<span class="number">280</span>);</span><br><span class="line">		f.setResizable(<span class="keyword">false</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> 单选和复选按钮();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/d5a8174eeeac14edb4e1a092aec300b3.png"></p>
<h3 id="下拉框、下拉列表列表和滚动条"><a href="#下拉框、下拉列表列表和滚动条" class="headerlink" title="下拉框、下拉列表列表和滚动条"></a>下拉框、下拉列表列表和滚动条</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下拉框</span></span><br><span class="line"><span class="comment">/*getSelectedItem()的方法得到选择的值和一个getSelectedIndex()得到选择值的索引*/</span></span><br><span class="line">String str[]=&#123;<span class="string">&quot;北京&quot;</span>,<span class="string">&quot;天津&quot;</span>,<span class="string">&quot;上海&quot;</span>,<span class="string">&quot;长沙&quot;</span>&#125;;</span><br><span class="line">JComboBox xlk=<span class="keyword">new</span> JComboBox(str);</span><br><span class="line"></span><br><span class="line"><span class="comment">//列表</span></span><br><span class="line">JPanel p=<span class="keyword">new</span> JPanel();</span><br><span class="line">String xl[]= &#123;<span class="string">&quot;高中&quot;</span>,<span class="string">&quot;大专&quot;</span>,<span class="string">&quot;本科&quot;</span>,<span class="string">&quot;硕士&quot;</span>,<span class="string">&quot;博士&quot;</span>&#125;;</span><br><span class="line">JList lb=<span class="keyword">new</span> JList(xl);</span><br><span class="line">lb.setVisibleRowCount(<span class="number">3</span>);<span class="comment">//使其滚动  3表示默认显示几个</span></span><br><span class="line">JScrollPane gd=<span class="keyword">new</span> JScrollPane(lb);   <span class="comment">//列表加入滚动</span></span><br><span class="line">p.add(xlk);</span><br><span class="line">p.add(gd); <span class="comment">//面板是添加滚动条。</span></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Windos;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="comment">/*下拉</span></span><br><span class="line"><span class="comment"> * JComboBox   创建字符组</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * 滚动条</span></span><br><span class="line"><span class="comment"> * JList  创建字符串组放进去 调用 setVisibleRowCount(3)</span></span><br><span class="line"><span class="comment"> * JScrollPane 放JList</span></span><br><span class="line"><span class="comment"> * 添加滚动</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 下拉列表和滚动条 </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JPanel p1,p2;</span><br><span class="line">	JLabel l1,l2;</span><br><span class="line">	</span><br><span class="line">	JComboBox xlk;  <span class="comment">//下拉框</span></span><br><span class="line">	<span class="comment">/*getSelectedItem()的方法得到选择的值和一个getSelectedIndex()得到选择值的索引*/</span></span><br><span class="line">	</span><br><span class="line">	JList lb;   <span class="comment">//列表</span></span><br><span class="line">	</span><br><span class="line">	JScrollPane gd;  <span class="comment">//滚动条</span></span><br><span class="line">	<span class="keyword">public</span> 下拉列表和滚动条() &#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame();</span><br><span class="line">		p1=<span class="keyword">new</span> JPanel();</span><br><span class="line">		p2=<span class="keyword">new</span> JPanel();</span><br><span class="line">		l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;籍贯&quot;</span>);</span><br><span class="line">		l2=<span class="keyword">new</span> JLabel(<span class="string">&quot;学历&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		String jg[]= &#123;<span class="string">&quot;北京&quot;</span>,<span class="string">&quot;天津&quot;</span>,<span class="string">&quot;长沙&quot;</span>,<span class="string">&quot;湖南&quot;</span>,<span class="string">&quot;安化&quot;</span>,<span class="string">&quot;梅城&quot;</span>&#125;;</span><br><span class="line">		xlk=<span class="keyword">new</span> JComboBox(jg);</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		String xl[]= &#123;<span class="string">&quot;高中&quot;</span>,<span class="string">&quot;大专&quot;</span>,<span class="string">&quot;本科&quot;</span>,<span class="string">&quot;硕士&quot;</span>,<span class="string">&quot;博士&quot;</span>&#125;;</span><br><span class="line">		lb=<span class="keyword">new</span> JList(xl);</span><br><span class="line">		</span><br><span class="line">		lb.setVisibleRowCount(<span class="number">3</span>);<span class="comment">//使其滚动  3表示默认显示几个</span></span><br><span class="line">		gd=<span class="keyword">new</span> JScrollPane(lb);   <span class="comment">//列表加入滚动</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		f.setLayout(<span class="keyword">new</span> GridLayout(<span class="number">2</span>,<span class="number">1</span>));</span><br><span class="line">		</span><br><span class="line">		p1.add(l1);</span><br><span class="line">		p1.add(xlk);</span><br><span class="line">		p2.add(l2);</span><br><span class="line">		p2.add(gd);<span class="comment">//添加滚动条</span></span><br><span class="line">		f.add(p1);</span><br><span class="line">		f.add(p2);</span><br><span class="line">		</span><br><span class="line">		f.setTitle(<span class="string">&quot;用户调查&quot;</span>);</span><br><span class="line">		f.setSize(<span class="number">200</span>,<span class="number">190</span>);</span><br><span class="line">		f.setLocation(<span class="number">300</span>,<span class="number">200</span>);</span><br><span class="line">		f.setResizable(<span class="keyword">false</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> 下拉列表和滚动条();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/3922831dbe80fb1208cc7db445adb582.png"></p>
<h3 id="拆分窗口"><a href="#拆分窗口" class="headerlink" title="拆分窗口"></a>拆分窗口</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Windos;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="comment">//图片 Ctrl+C 复制 工程文件右击粘贴 可新建一个文件夹放图片</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 拆分窗口 </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JSplitPane cf; <span class="comment">//拆分成左右两个界面</span></span><br><span class="line">	JList lb;</span><br><span class="line">	JLabel l;</span><br><span class="line">	String str[]= &#123;<span class="string">&quot;软件开发&quot;</span>,<span class="string">&quot;游戏开发&quot;</span>,<span class="string">&quot;平面设计&quot;</span>,<span class="string">&quot;动画制作&quot;</span>,<span class="string">&quot;室内设计&quot;</span>&#125;;</span><br><span class="line">	<span class="keyword">public</span>  拆分窗口() &#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame();</span><br><span class="line">		</span><br><span class="line">		lb=<span class="keyword">new</span> JList(str);</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		l=<span class="keyword">new</span> JLabel(<span class="keyword">new</span> ImageIcon(<span class="string">&quot;Image/雪花.jpg&quot;</span>)); <span class="comment">//放java工程路径</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		cf=<span class="keyword">new</span> JSplitPane(JSplitPane.HORIZONTAL_SPLIT,lb,l);<span class="comment">//左右拆分</span></span><br><span class="line">	<span class="comment">//	cf=new JSplitPane(JSplitPane.VERTICAL_SPLIT,lb,l);//上下拆分</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		cf.setOneTouchExpandable(<span class="keyword">true</span>); <span class="comment">//出现两个拉动箭头 可以覆盖或收缩掉一界面</span></span><br><span class="line">		</span><br><span class="line">		f.add(cf);</span><br><span class="line">		</span><br><span class="line">		f.setSize(<span class="number">640</span>,<span class="number">480</span>);</span><br><span class="line">		f.setLocation(<span class="number">300</span>,<span class="number">280</span>);</span><br><span class="line">		f.setResizable(<span class="keyword">false</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">	  <span class="keyword">new</span> 拆分窗口();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/f8ed91217debb2e4a113daefc8d04903.png"></p>
<h2 id="布局管理"><a href="#布局管理" class="headerlink" title="布局管理"></a>布局管理</h2><h3 id="绝对布局"><a href="#绝对布局" class="headerlink" title="绝对布局"></a>绝对布局</h3><p>绝对布局指硬性指定组件在容器中的位置和大小，可以使用绝对坐标的方式来指定组件的位置。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">使用步骤:</span></span><br><span class="line"><span class="comment">1.使用Container.setLayout(null)方法取消布局管理器。</span></span><br><span class="line"><span class="comment">2.使用Container.setBounds()方法设置每个组件的大小与位置。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">package</span> Blog_02;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">blog_02_07</span> <span class="keyword">extends</span> <span class="title">JFrame</span></span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">blog_02_07</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		setTitle(<span class="string">&quot;绝对布局&quot;</span>);</span><br><span class="line">		setLayout(<span class="keyword">null</span>);</span><br><span class="line">		setBounds(<span class="number">0</span>,<span class="number">0</span>,<span class="number">200</span>,<span class="number">150</span>);</span><br><span class="line">		Container c=getContentPane();</span><br><span class="line">		JButton b1=<span class="keyword">new</span> JButton(<span class="string">&quot;按钮1&quot;</span>);</span><br><span class="line">		JButton b2=<span class="keyword">new</span> JButton(<span class="string">&quot;按钮2&quot;</span>);</span><br><span class="line">		b1.setBounds(<span class="number">10</span>,<span class="number">30</span>,<span class="number">80</span>,<span class="number">30</span>);</span><br><span class="line">		b2.setBounds(<span class="number">60</span>,<span class="number">70</span>,<span class="number">100</span>,<span class="number">20</span>);</span><br><span class="line">		c.add(b1);</span><br><span class="line">		c.add(b2);</span><br><span class="line">		setVisible(<span class="keyword">true</span>);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="keyword">new</span> blog_02_07();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/143377b7b2dc142b6cddbebffb2630c8.png"></p>
<h3 id="流动布局"><a href="#流动布局" class="headerlink" title="流动布局"></a>流动布局</h3><p>流动布局指组件像“流”一样从左到右摆放组件。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Windos;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FlowLayout_1</span> </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	</span><br><span class="line">	JButton b[]; <span class="comment">//按钮数组</span></span><br><span class="line">	String str[]= &#123;<span class="string">&quot;话梅&quot;</span>,<span class="string">&quot;果脯&quot;</span>,<span class="string">&quot;薯片&quot;</span>,<span class="string">&quot;饼干&quot;</span>,<span class="string">&quot;巧克力&quot;</span>,<span class="string">&quot;腰果&quot;</span>,<span class="string">&quot;锅巴&quot;</span>,<span class="string">&quot;开心果&quot;</span>&#125;; <span class="comment">//字符串数组</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">FlowLayout_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;流动窗口&quot;</span>);</span><br><span class="line">		b=<span class="keyword">new</span> JButton[str.length]; <span class="comment">//等价于 JButton b[]=new JButton[str.length]  相当于创建按键数组</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;str.length;i++) &#123;</span><br><span class="line">			b[i]=<span class="keyword">new</span> JButton(str[i]); <span class="comment">//一个一个new</span></span><br><span class="line">			</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//网格布局方式  java默认边界布局管理</span></span><br><span class="line">		f.setLayout(<span class="keyword">new</span> FlowLayout());</span><br><span class="line">		<span class="comment">//f.setLayout(new FlowLayout(FlowLayout.LEFT));//向左对齐</span></span><br><span class="line">		<span class="comment">//f.setLayout(new FlowLayout(FlowLayout.RIGHT));//向右看齐</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;str.length;i++) &#123;</span><br><span class="line">			f.add(b[i]);</span><br><span class="line">		&#125;</span><br><span class="line">		f.setSize(<span class="number">300</span>,<span class="number">150</span>);</span><br><span class="line">		f.setLocation(<span class="number">200</span>,<span class="number">200</span>);</span><br><span class="line">		f.setResizable(<span class="keyword">false</span>);<span class="comment">//不能最大化 拖动</span></span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> FlowLayout_1();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/91cc9c0cb639ebd1108108d89a2743e5.png"></p>
<h3 id="边界布局"><a href="#边界布局" class="headerlink" title="边界布局"></a>边界布局</h3><p>边界布局将容器划分东、南、西、北、中五个区域。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Windos;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BorderLayout_1</span> </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JButton b,b1,b2,b3,b4;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">BorderLayout_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;边界布局&quot;</span>);</span><br><span class="line">		b=<span class="keyword">new</span> JButton(<span class="string">&quot;东方&quot;</span>);</span><br><span class="line">		b1=<span class="keyword">new</span> JButton(<span class="string">&quot;西方&quot;</span>);</span><br><span class="line">		b2=<span class="keyword">new</span> JButton(<span class="string">&quot;南方&quot;</span>);</span><br><span class="line">		b3=<span class="keyword">new</span> JButton(<span class="string">&quot;北方&quot;</span>);</span><br><span class="line">		b4=<span class="keyword">new</span> JButton(<span class="string">&quot;中方&quot;</span>);</span><br><span class="line">		<span class="comment">//边界布局方式</span></span><br><span class="line">		f.add(b,BorderLayout.EAST);</span><br><span class="line">		f.add(b1,BorderLayout.WEST);</span><br><span class="line">		f.add(b2,BorderLayout.SOUTH);</span><br><span class="line">		f.add(b3,BorderLayout.NORTH);</span><br><span class="line">		f.add(b4,BorderLayout.CENTER);</span><br><span class="line">		<span class="comment">//如果不是5个按钮，则会以扩充中部去填补</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//使任务管理器中窗口也关闭 ，不占内存 很重要！！！！！</span></span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<span class="comment">//使运行后的窗口不占内存  起关闭作用</span></span><br><span class="line">		<span class="comment">///</span></span><br><span class="line">		</span><br><span class="line">		f.setSize(<span class="number">300</span>,<span class="number">400</span>);</span><br><span class="line">		f.setLocation(<span class="number">200</span>,<span class="number">200</span>);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> BorderLayout_1();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/b1c0d8b1f554da1fc04211f71347eea4.png"></p>
<h3 id="网格布局"><a href="#网格布局" class="headerlink" title="网格布局"></a>网格布局</h3><p>网格布局将容器划分为网格，所有组件按行和列排列。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Windos;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GridLayout_1</span> </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JButton b[];</span><br><span class="line">	String str[]= &#123;<span class="string">&quot;话梅&quot;</span>,<span class="string">&quot;果脯&quot;</span>,<span class="string">&quot;薯片&quot;</span>,<span class="string">&quot;饼干&quot;</span>,<span class="string">&quot;巧克力&quot;</span>,<span class="string">&quot;腰果&quot;</span>,<span class="string">&quot;锅巴&quot;</span>,<span class="string">&quot;开心果&quot;</span>,<span class="string">&quot;蛋糕&quot;</span>&#125;; <span class="comment">//字符串数组</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">GridLayout_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;网格布局&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		b=<span class="keyword">new</span> JButton[str.length]; <span class="comment">//等价于JButton b[]=new JButton[大小]  创建数组</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;str.length;i++) &#123;</span><br><span class="line">			b[i]=<span class="keyword">new</span> JButton(str[i]);  <span class="comment">//一个一个添加</span></span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//网格布局格式</span></span><br><span class="line">		f.setLayout(<span class="keyword">new</span> GridLayout(<span class="number">3</span>,<span class="number">3</span>,<span class="number">12</span>,<span class="number">13</span>)); <span class="comment">//(行，列，长高的像素空间)</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;str.length;i++) &#123;</span><br><span class="line">			f.add(b[i]);</span><br><span class="line">		&#125;</span><br><span class="line">		f.setSize(<span class="number">380</span>,<span class="number">320</span>);</span><br><span class="line">	    f.setLocation(<span class="number">200</span>,<span class="number">200</span>);</span><br><span class="line">	    </span><br><span class="line">	    f.setResizable(<span class="keyword">false</span>);<span class="comment">//不能最大化 不能拉伸窗口 使其固定</span></span><br><span class="line">	    </span><br><span class="line">	    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">	    f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String []args)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">new</span> GridLayout_1();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/8fd9c2b221ae6f0801a34bb6d43e119d.png"></p>
<h3 id="盒式布局"><a href="#盒式布局" class="headerlink" title="盒式布局"></a>盒式布局</h3><p><code>BoxLayout </code>可以把控件依次进行水平或者垂直排列布局。<br>create<code>HorizontalBox</code>()：返回一个 Box 对象，它采用水平 BoxLayout，即 BoxLayout 沿着水平方向放置组件，让组件在容器内从<code>左到右</code>排列。<br>create<code>VerticalBox</code>()：返回一个 Box 对象，它采用垂直 BoxLayout，即 BoxLayout 沿着垂直方向放置组件，让组件在容器内从<code>上到下</code>进行排列。<br>Box 布局时，可以使用空隙类来调整和控制组件之间的间隔，使组件布局达到要求。<br>空隙类的对象是一种占用空间但不可见的组件。空隙类的对象有两种：<br>支撑组件（<code>Struct</code>）：用于调整组件间的有指定长度的间距<br>1.createVerticalStrut(int height) 2.createHorizontalStrut(int width)<br>胶水组件（<code>Glue</code>）：用于调整组件间的无指定长度的间距<br>1.createHorizontalGlue() 2.createVerticalGlue()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//垂直盒子/水平盒子</span></span><br><span class="line"><span class="keyword">package</span> Swing_Layout;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BoxLayout_1</span> </span>&#123;</span><br><span class="line">		JFrame f;</span><br><span class="line">		JButton b1,b2,b3;</span><br><span class="line">		Box box;</span><br><span class="line">		</span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="title">BoxLayout_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			f=<span class="keyword">new</span> JFrame(<span class="string">&quot;盒式布局&quot;</span>);</span><br><span class="line">			b1=<span class="keyword">new</span> JButton(<span class="string">&quot;按钮1&quot;</span>);</span><br><span class="line">			b2=<span class="keyword">new</span> JButton(<span class="string">&quot;按钮2&quot;</span>);</span><br><span class="line">			b3=<span class="keyword">new</span> JButton(<span class="string">&quot;按钮3&quot;</span>);</span><br><span class="line">			box=Box.createVerticalBox();<span class="comment">//垂直盒子</span></span><br><span class="line">		<span class="comment">//	box=Box.createHorizontalBox()//水平盒子</span></span><br><span class="line">			box.add(b1);</span><br><span class="line">			box.add(Box.createGlue());</span><br><span class="line">		<span class="comment">//	box.add(Box.createVerticalStrut(10));</span></span><br><span class="line">			box.add(b2);</span><br><span class="line">			box.add(Box.createGlue());</span><br><span class="line">			box.add(b3);</span><br><span class="line">			</span><br><span class="line">			f.add(box);</span><br><span class="line">			<span class="comment">//f.pack();</span></span><br><span class="line">			f.setSize(<span class="number">300</span>,<span class="number">400</span>);</span><br><span class="line">			f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">			f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> BoxLayout_1();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/5a7482a37413c82fc2ace16801d75511.png"><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/9df4f6c4f24517fb0023317a90010328.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//改变间隔</span></span><br><span class="line"><span class="keyword">package</span> Swing_Layout;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BoxLayout_3</span> </span>&#123;</span><br><span class="line">	 JFrame f;</span><br><span class="line">	 JButton b;</span><br><span class="line">	 Box box;</span><br><span class="line">	 String name[]= &#123;<span class="string">&quot;按钮1&quot;</span>,<span class="string">&quot;按钮2&quot;</span>,<span class="string">&quot;按钮3&quot;</span>&#125;;</span><br><span class="line">	 <span class="function"><span class="keyword">public</span> <span class="title">BoxLayout_3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		 f=<span class="keyword">new</span> JFrame();</span><br><span class="line">		 box=Box.createVerticalBox();</span><br><span class="line">		 <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;name.length;i++) &#123;</span><br><span class="line">			 b=<span class="keyword">new</span> JButton(name[i]);</span><br><span class="line">			 <span class="keyword">if</span>(i==<span class="number">1</span>)</span><br><span class="line">				 box.add(Box.createVerticalStrut(<span class="number">10</span>));</span><br><span class="line">			 <span class="keyword">if</span>(i==<span class="number">2</span>)</span><br><span class="line">				 box.add(Box.createVerticalStrut(<span class="number">20</span>));</span><br><span class="line">			 box.add(b);</span><br><span class="line">		 &#125;</span><br><span class="line">		 f.add(box);</span><br><span class="line">		 f.setSize(<span class="number">300</span>,<span class="number">400</span>);</span><br><span class="line">		 f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">		 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		 f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	 &#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> BoxLayout_3();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/a635d72d75d4c2d0c61cab949c39312e.png"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Swing_Layout;</span><br><span class="line"><span class="keyword">import</span> java.awt.BorderLayout;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BoxLayout_2</span> </span>&#123;</span><br><span class="line">		JFrame f;</span><br><span class="line">		JTextField t;</span><br><span class="line">		JTextArea jt;</span><br><span class="line">		JLabel l1,l2;</span><br><span class="line">		JButton b;</span><br><span class="line">		JScrollPane gd;</span><br><span class="line">		Box H1,H2,V1;</span><br><span class="line">		<span class="comment">//水平 水平  垂直</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="title">BoxLayout_2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			f=<span class="keyword">new</span> JFrame();</span><br><span class="line">			</span><br><span class="line">			<span class="comment">//主题</span></span><br><span class="line">			l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;主题:&quot;</span>);</span><br><span class="line">			t=<span class="keyword">new</span> JTextField(<span class="number">10</span>);</span><br><span class="line">			H1=Box.createHorizontalBox();<span class="comment">//水平盒子</span></span><br><span class="line">			H1.add(Box.createHorizontalStrut(<span class="number">5</span>));<span class="comment">//离布局间距</span></span><br><span class="line">			H1.add(l1);</span><br><span class="line">			H1.add(Box.createHorizontalStrut(<span class="number">5</span>));<span class="comment">//水平间距</span></span><br><span class="line">			H1.add(t);</span><br><span class="line">			</span><br><span class="line">	</span><br><span class="line">			<span class="comment">//setAlignmentX()用于使用了BoxLayout.Y_AXIS时，将控件在X轴方向设置为左对齐、右对齐或居中对</span></span><br><span class="line">			<span class="comment">//setAlignmentY()将控件在Y轴方向设置为顶对齐、底对齐或居中对齐</span></span><br><span class="line">			<span class="comment">//内容</span></span><br><span class="line">			l2=<span class="keyword">new</span> JLabel(<span class="string">&quot;内容:&quot;</span>);</span><br><span class="line">			jt=<span class="keyword">new</span> JTextArea();</span><br><span class="line">			gd=<span class="keyword">new</span> JScrollPane(jt);</span><br><span class="line">			H2=Box.createHorizontalBox();<span class="comment">//水平</span></span><br><span class="line">			H2.setAlignmentX(<span class="number">1</span>);  <span class="comment">//设置靠右对齐 X水平轴</span></span><br><span class="line">			H2.add(Box.createHorizontalStrut(<span class="number">5</span>));<span class="comment">//边界间距</span></span><br><span class="line">			l2.setAlignmentY(<span class="number">0</span>);<span class="comment">//设置标签垂直靠上对齐</span></span><br><span class="line">			H2.add(l2);</span><br><span class="line">			H2.add(Box.createHorizontalStrut(<span class="number">5</span>));<span class="comment">//间距</span></span><br><span class="line">			gd.setAlignmentY(<span class="number">0</span>); <span class="comment">//文本域垂直靠上</span></span><br><span class="line">			H2.add(gd);</span><br><span class="line">			<span class="comment">//</span></span><br><span class="line">			<span class="comment">//垂直</span></span><br><span class="line">			<span class="comment">//H2和按键垂直</span></span><br><span class="line">			V1=Box.createVerticalBox();</span><br><span class="line">			V1.add(Box.createVerticalStrut(<span class="number">5</span>));<span class="comment">//和H1间距</span></span><br><span class="line">			V1.add(H2);</span><br><span class="line">			<span class="comment">//确定</span></span><br><span class="line">			jt.setLineWrap(<span class="keyword">true</span>);</span><br><span class="line">			gd.setViewportView(jt);</span><br><span class="line">			V1.add(Box.createVerticalStrut(<span class="number">5</span>));</span><br><span class="line">			</span><br><span class="line">			b=<span class="keyword">new</span> JButton(<span class="string">&quot;确定&quot;</span>);</span><br><span class="line">			b.setAlignmentX(<span class="number">1</span>);  <span class="comment">//水平靠右</span></span><br><span class="line">			V1.add(b);</span><br><span class="line">			</span><br><span class="line">			f.add(H1,BorderLayout.NORTH);<span class="comment">//主题</span></span><br><span class="line">			f.add(V1,BorderLayout.CENTER);<span class="comment">//垂直加中间</span></span><br><span class="line">		</span><br><span class="line">			f.setSize(<span class="number">400</span>,<span class="number">500</span>);</span><br><span class="line">			f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">			f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span>  BoxLayout_2();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/4783a159c0548e8d5d9d29d6d4fecc67.png"></p>
<h3 id="卡片布局"><a href="#卡片布局" class="headerlink" title="卡片布局"></a>卡片布局</h3><p><code>卡片布局</code>可以通过点击窗口中的按钮切换不同的面板。像卡片一样叠加，一张接着一张的显示</p>
<p><code>CardLayout成员方法</code>:<br>JPanel cp=new JPanel();<br>CardLayout c=new CardLayout();<br>c.<code>first</code>(cp):显示第一个卡片<br>c.<code>last</code>(cp)：显示最后一个卡片<br>c.<code>next</code>(cp)：显示当前所显示卡片之后的卡片<br>c.<code>previous</code>(cp)：显示当前所显示卡片之前的卡片<br>c.<code>show</code>(cp,String name)：显示指定标签的卡片</p>
<p>卡片布局使用步骤:<br>1.<code>创建CardLayout布局对象</code><br>CardLayout l = new CardLayout();<br>2.<code>创建容器对象</code><br>JPanel panel = new JPanel();<br>3.<code>设置容器对象的布局或使用默认布局</code><br>panel.setLayout(l);<br>4.<code>向容器中添加组件对象(设组件对象已创建)</code><br>panel.add(组件对象，“名称”);<br>5.<code>显示组件</code><br>l.show(panel,  “名称”);<br>l.first(panel);<br> l.last(panel);<br>l.next(panel);<br>l.previous(panel);</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Swing_Layout;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CardLayout_1</span> </span>&#123;</span><br><span class="line">		 JFrame f;</span><br><span class="line">		 <span class="comment">//卡片布局面板</span></span><br><span class="line">		 JPanel cp;</span><br><span class="line">		 CardLayout c;</span><br><span class="line">		 JLabel l;</span><br><span class="line">		 <span class="comment">//控制面板</span></span><br><span class="line">		 JPanel bp; <span class="comment">//按键面板</span></span><br><span class="line">		 JButton b; </span><br><span class="line">		 </span><br><span class="line">		 <span class="function"><span class="keyword">public</span> <span class="title">CardLayout_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			 f=<span class="keyword">new</span> JFrame();</span><br><span class="line">			 <span class="comment">//卡片布局设置</span></span><br><span class="line">			 cp=<span class="keyword">new</span> JPanel();</span><br><span class="line">			 c=<span class="keyword">new</span> CardLayout();</span><br><span class="line">			 cp.setLayout(c);</span><br><span class="line">			 f.add(cp,BorderLayout.CENTER);</span><br><span class="line">			 <span class="comment">//标签</span></span><br><span class="line">			 String lname[]= &#123;<span class="string">&quot;卡片A&quot;</span>,<span class="string">&quot;卡片B&quot;</span>,<span class="string">&quot;卡片C&quot;</span>&#125;;</span><br><span class="line">			 </span><br><span class="line">			 <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;lname.length;i++) &#123;</span><br><span class="line">				 l=<span class="keyword">new</span> JLabel(lname[i]);</span><br><span class="line">				 l.setHorizontalAlignment(SwingConstants.CENTER);</span><br><span class="line">				 l.setFont(<span class="keyword">new</span> Font(<span class="string">&quot;楷书&quot;</span>,Font.BOLD,<span class="number">16</span>));</span><br><span class="line">				 l.setForeground(<span class="keyword">new</span> Color(<span class="number">255</span>,<span class="number">0</span>,<span class="number">0</span>));</span><br><span class="line">				<span class="comment">//像采用卡片布局的面板中加卡片</span></span><br><span class="line">				cp.add(l,lname[i]);</span><br><span class="line">			 &#125;</span><br><span class="line">			</span><br><span class="line">			  bp=<span class="keyword">new</span> JPanel();</span><br><span class="line">			 f.add(bp,BorderLayout.SOUTH);</span><br><span class="line">			 </span><br><span class="line">			 </span><br><span class="line">			 String bname[]= &#123;<span class="string">&quot;第一个&quot;</span>,<span class="string">&quot;前一个&quot;</span>,<span class="string">&quot;卡片A&quot;</span>,<span class="string">&quot;卡片B&quot;</span>,<span class="string">&quot;卡片C&quot;</span>,<span class="string">&quot;后一个&quot;</span>,<span class="string">&quot;最后一个&quot;</span>&#125;;</span><br><span class="line">			 <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;bname.length;i++) &#123;</span><br><span class="line">				 JButton b=<span class="keyword">new</span> JButton(bname[i]);</span><br><span class="line">				 bp.add(b);</span><br><span class="line">				 b.addActionListener(<span class="keyword">new</span> ActionListener()&#123;</span><br><span class="line"></span><br><span class="line">					<span class="meta">@Override</span></span><br><span class="line">					<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">						<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">							String event=b.getText();</span><br><span class="line">							</span><br><span class="line">							<span class="keyword">if</span>(event.equals(<span class="string">&quot;第一个&quot;</span>)) &#123;</span><br><span class="line">								c.first(cp);</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(event.equals(<span class="string">&quot;前一个&quot;</span>)) &#123;</span><br><span class="line">								c.previous(cp);</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(event.equals(<span class="string">&quot;卡片A&quot;</span>)) &#123;</span><br><span class="line">								c.show(cp,<span class="string">&quot;卡片A&quot;</span>);</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(event.equals(<span class="string">&quot;卡片B&quot;</span>)) &#123;</span><br><span class="line">								c.show(cp,<span class="string">&quot;卡片B&quot;</span>);</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(event.equals(<span class="string">&quot;卡片C&quot;</span>)) &#123;</span><br><span class="line">								c.show(cp,<span class="string">&quot;卡片C&quot;</span>);</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(event.equals(<span class="string">&quot;后一个&quot;</span>)) &#123;</span><br><span class="line">								c.next(cp);</span><br><span class="line">							&#125;<span class="keyword">else</span> <span class="keyword">if</span>(event.equals(<span class="string">&quot;最后一个&quot;</span>)) &#123;</span><br><span class="line">								c.last(cp);</span><br><span class="line">							&#125;</span><br><span class="line">					&#125;&#125;);</span><br><span class="line">			 &#125;</span><br><span class="line">			</span><br><span class="line">			 f.pack();</span><br><span class="line">			 f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">			 f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		 &#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> CardLayout_1();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">​```java</span><br><span class="line"><span class="comment">//利用卡片布局实训相册</span></span><br><span class="line">![](https:<span class="comment">//cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/e348f8e551bc264d616c873b59e80976.png)</span></span><br><span class="line">​```java</span><br><span class="line"><span class="keyword">package</span> Swing_Layout;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span>  java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CardLayout_2</span> </span>&#123;</span><br><span class="line">		JFrame f;</span><br><span class="line">		JLabel l;</span><br><span class="line">		JPanel cp,p;</span><br><span class="line">		CardLayout c;</span><br><span class="line">		ImageIcon img;</span><br><span class="line">		String imgname[]= &#123;<span class="string">&quot;Image/1.jpg&quot;</span>,<span class="string">&quot;Image/2.jpg&quot;</span>,<span class="string">&quot;Image/3.jpg&quot;</span>,<span class="string">&quot;Image/4.jpg&quot;</span>&#125;;</span><br><span class="line">		String bname[]= &#123;<span class="string">&quot;第一个&quot;</span>,<span class="string">&quot;前一个&quot;</span>,<span class="string">&quot;图片A&quot;</span>,<span class="string">&quot;图片B&quot;</span>,<span class="string">&quot;图片C&quot;</span>,<span class="string">&quot;图片D&quot;</span>,<span class="string">&quot;后一个&quot;</span>,<span class="string">&quot;最后一个&quot;</span>&#125;;</span><br><span class="line">		String imgname2[]= &#123;<span class="string">&quot;图片A&quot;</span>,<span class="string">&quot;图片B&quot;</span>,<span class="string">&quot;图片C&quot;</span>,<span class="string">&quot;图片D&quot;</span>&#125;;</span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="title">CardLayout_2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			f=<span class="keyword">new</span> JFrame();</span><br><span class="line">			cp=<span class="keyword">new</span> JPanel();</span><br><span class="line">			c=<span class="keyword">new</span> CardLayout();</span><br><span class="line">			cp.setLayout(c);</span><br><span class="line">			f.add(cp);</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;imgname.length;i++) &#123;</span><br><span class="line">				img=<span class="keyword">new</span> ImageIcon(imgname[i]);<span class="comment">//创建图片</span></span><br><span class="line">				l=<span class="keyword">new</span> JLabel(img);<span class="comment">//创建标签</span></span><br><span class="line">				</span><br><span class="line">				<span class="comment">//像采用卡片布局的面板中加卡片</span></span><br><span class="line">				<span class="comment">//并为卡片设置名字</span></span><br><span class="line">				cp.add(l,imgname2[i]);</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			p=<span class="keyword">new</span> JPanel();</span><br><span class="line">			f.add(p,BorderLayout.SOUTH);</span><br><span class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;bname.length;i++) &#123;</span><br><span class="line">				JButton b=<span class="keyword">new</span> JButton(bname[i]);<span class="comment">//防止局部变量</span></span><br><span class="line">				p.add(b);</span><br><span class="line">				b.addActionListener(<span class="keyword">new</span> ActionListener() &#123;</span><br><span class="line"></span><br><span class="line">					<span class="meta">@Override</span></span><br><span class="line">					<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">						<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">						String event=b.getText();<span class="comment">//按键值</span></span><br><span class="line">						<span class="keyword">if</span>(event.equals(<span class="string">&quot;第一个&quot;</span>))&#123;</span><br><span class="line">							c.first(cp);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;前一个&quot;</span>)) &#123;</span><br><span class="line">							c.previous(cp);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;图片A&quot;</span>)) &#123;</span><br><span class="line">							c.show(cp,<span class="string">&quot;图片A&quot;</span>);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;图片B&quot;</span>)) &#123;</span><br><span class="line">							c.show(cp, <span class="string">&quot;图片B&quot;</span>);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;图片C&quot;</span>)) &#123;</span><br><span class="line">							c.show(cp, <span class="string">&quot;图片C&quot;</span>);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;图片D&quot;</span>)) &#123;</span><br><span class="line">							c.show(cp, <span class="string">&quot;图片D&quot;</span>);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;后一个&quot;</span>)) &#123;</span><br><span class="line">							c.next(cp);</span><br><span class="line">						&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;最后一个&quot;</span>)) &#123;</span><br><span class="line">							c.last(cp);</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					</span><br><span class="line">				&#125;);</span><br><span class="line">			&#125;</span><br><span class="line">			f.pack();</span><br><span class="line">			f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">			f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> CardLayout_2();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/25/26bef4624ae4de3d1529e8584a3d70e7.png"></p>
<h1 id="Swing高级"><a href="#Swing高级" class="headerlink" title="Swing高级"></a>Swing高级</h1><h2 id="JTable表格"><a href="#JTable表格" class="headerlink" title="JTable表格"></a>JTable表格</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">一.创建表格控件的各种方式:</span><br><span class="line">(<span class="number">1</span>)  调用无参构造函数.</span><br><span class="line">JTable table = <span class="keyword">new</span> JTable();</span><br><span class="line">(<span class="number">2</span>)  以表头和表数据创建表格.</span><br><span class="line">String columnNames[]= &#123;<span class="string">&quot;姓名&quot;</span>,<span class="string">&quot;性别&quot;</span>,<span class="string">&quot;年龄&quot;</span>,<span class="string">&quot;政治面貌&quot;</span>&#125;;</span><br><span class="line">Object data[][]= &#123;&#123;<span class="string">&quot;陈峰&quot;</span>,<span class="string">&quot;男&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">19</span>),<span class="string">&quot;党员&quot;</span>&#125;,</span><br><span class="line">	           &#123;<span class="string">&quot;田一飞&quot;</span>,<span class="string">&quot;男&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">18</span>),<span class="string">&quot;团员&quot;</span>&#125;,</span><br><span class="line">	            &#123;<span class="string">&quot;胡锦&quot;</span>,<span class="string">&quot;女&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">19</span>),<span class="string">&quot;党员&quot;</span>&#125;&#125;;</span><br><span class="line">JTable table = <span class="keyword">new</span> JTable(data, columnNames);</span><br><span class="line">JTableHeader tablerHeader=table.getTableHeader(); <span class="comment">//获取表格列名.</span></span><br><span class="line">(<span class="number">3</span>)利用表格模型创建表格</span><br><span class="line">DefaultTableModel tableModel=<span class="keyword">new</span> DefaultTableMode();<span class="comment">//创建表格模型</span></span><br><span class="line">JTable table=<span class="keyword">new</span> JTabel(tableModel); <span class="comment">//创建表格</span></span><br><span class="line">table.setRowSorter(<span class="keyword">new</span> TableRowSorter(tableModel)); <span class="comment">//设置排序器</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Swing_list;</span><br><span class="line"><span class="keyword">import</span> java.awt.BorderLayout;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.MouseAdapter;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.MouseEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.table.DefaultTableModel;</span><br><span class="line"><span class="keyword">import</span> javax.swing.table.TableRowSorter;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 默认方法创建  </span>&#123;</span><br><span class="line">		JFrame f;</span><br><span class="line">		JTable table;</span><br><span class="line">		JLabel l1,l2,l3,l4,l5;</span><br><span class="line">		JButton b1,b2,b3;</span><br><span class="line">		JTextField t1,t2,t3,t4;</span><br><span class="line">		JPanel p;</span><br><span class="line">		JScrollPane gd;</span><br><span class="line">		DefaultTableModel tableModel;</span><br><span class="line">		String Listname[]= &#123;<span class="string">&quot;学号&quot;</span>,<span class="string">&quot;姓名&quot;</span>,<span class="string">&quot;年龄&quot;</span>,<span class="string">&quot;专业&quot;</span>&#125;;</span><br><span class="line">		Object[][] date= &#123;&#123;<span class="string">&quot;20172001&quot;</span>,<span class="string">&quot;张三&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">18</span>),<span class="string">&quot;软件工程&quot;</span>&#125;,</span><br><span class="line">							&#123;<span class="string">&quot;20172002&quot;</span>,<span class="string">&quot;李四&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">19</span>),<span class="string">&quot;软件工程&quot;</span>&#125;,</span><br><span class="line">							&#123;<span class="string">&quot;20172003&quot;</span>,<span class="string">&quot;王五&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">19</span>),<span class="string">&quot;软件工程&quot;</span>&#125;,</span><br><span class="line">							&#123;<span class="string">&quot;20172004&quot;</span>,<span class="string">&quot;赵六&quot;</span>,<span class="keyword">new</span> Integer(<span class="number">18</span>),<span class="string">&quot;软件工程&quot;</span>&#125;&#125;;</span><br><span class="line">		<span class="keyword">public</span> 默认方法创建() &#123;</span><br><span class="line">			f=<span class="keyword">new</span> JFrame(<span class="string">&quot;表格&quot;</span>);</span><br><span class="line">			tableModel=<span class="keyword">new</span> DefaultTableModel(date,Listname);<span class="comment">//创建表格模型</span></span><br><span class="line">			table=<span class="keyword">new</span> JTable(tableModel);<span class="comment">//创建表格</span></span><br><span class="line">			gd=<span class="keyword">new</span> JScrollPane(table);<span class="comment">//创建滚动面板</span></span><br><span class="line">			table.setRowHeight(<span class="number">30</span>);</span><br><span class="line">			<span class="comment">//设置表格排序器</span></span><br><span class="line">			table.setRowSorter(<span class="keyword">new</span> TableRowSorter(tableModel));</span><br><span class="line">			<span class="comment">//设置表格的选择模式为单选</span></span><br><span class="line">			table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			<span class="comment">//为表格添加鼠标事件监听器</span></span><br><span class="line">			<span class="comment">//点击表格 修改框反应</span></span><br><span class="line">			table.addMouseListener(<span class="keyword">new</span> MouseAdapter() &#123;</span><br><span class="line">				<span class="comment">//发生了单击事件</span></span><br><span class="line">				<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">mouseClicked</span><span class="params">(MouseEvent e)</span> </span>&#123;</span><br><span class="line">					<span class="comment">//获得被选中行的索引</span></span><br><span class="line">					<span class="keyword">int</span> selectedRow=table.getSelectedRow(); <span class="comment">//行数</span></span><br><span class="line">					<span class="comment">//从表格模型中获得指定单元格的值</span></span><br><span class="line">					<span class="comment">//行数固定 改变列数来获取</span></span><br><span class="line">					Object a=tableModel.getValueAt(selectedRow, <span class="number">0</span>);<span class="comment">//行 列</span></span><br><span class="line">					Object b=tableModel.getValueAt(selectedRow, <span class="number">1</span>);</span><br><span class="line">					Object c=tableModel.getValueAt(selectedRow, <span class="number">2</span>);</span><br><span class="line">					Object d=tableModel.getValueAt(selectedRow, <span class="number">3</span>);</span><br><span class="line">					t1.setText(a.toString());  <span class="comment">//将值赋给文本框</span></span><br><span class="line">					t2.setText(b.toString());</span><br><span class="line">					t3.setText(c.toString());</span><br><span class="line">					t4.setText(d.toString());</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			</span><br><span class="line">			p=<span class="keyword">new</span> JPanel();</span><br><span class="line">			l1=<span class="keyword">new</span> JLabel(<span class="string">&quot;学号&quot;</span>,JLabel.CENTER);</span><br><span class="line">			l2=<span class="keyword">new</span> JLabel(<span class="string">&quot;姓名&quot;</span>,JLabel.CENTER);</span><br><span class="line">			l3=<span class="keyword">new</span> JLabel(<span class="string">&quot;年龄&quot;</span>,JLabel.CENTER);</span><br><span class="line">			l4=<span class="keyword">new</span> JLabel(<span class="string">&quot;专业&quot;</span>,JLabel.CENTER);</span><br><span class="line">			l5=<span class="keyword">new</span> JLabel(<span class="string">&quot;修改表格&quot;</span>,JLabel.CENTER);</span><br><span class="line">			t1=<span class="keyword">new</span> JTextField(<span class="number">10</span>);</span><br><span class="line">			t2=<span class="keyword">new</span> JTextField(<span class="number">10</span>);</span><br><span class="line">			t3=<span class="keyword">new</span> JTextField(<span class="number">10</span>);</span><br><span class="line">			t4=<span class="keyword">new</span> JTextField(<span class="number">10</span>);</span><br><span class="line">			</span><br><span class="line">			b1=<span class="keyword">new</span> JButton(<span class="string">&quot;添加&quot;</span>);</span><br><span class="line">			b1.addActionListener(<span class="keyword">new</span> ActionListener() &#123;</span><br><span class="line"></span><br><span class="line">				<span class="meta">@Override</span></span><br><span class="line">				<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">					<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">					<span class="comment">//创建表格行数组</span></span><br><span class="line">					String contents[]= &#123;t1.getText(),t2.getText(),t3.getText(),t4.getText()&#125;;</span><br><span class="line">					tableModel.addRow(contents);<span class="comment">//向表格模型中加一行</span></span><br><span class="line">					l5.setText(<span class="string">&quot;添加成功&quot;</span>);</span><br><span class="line">					l5.setForeground(Color.BLUE);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">			b2=<span class="keyword">new</span> JButton(<span class="string">&quot;修改&quot;</span>);</span><br><span class="line">			b2.addActionListener(<span class="keyword">new</span> ActionListener() &#123;</span><br><span class="line"></span><br><span class="line">				<span class="meta">@Override</span></span><br><span class="line">				<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">					<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">					<span class="keyword">int</span> selectedRow=table.getSelectedRow();<span class="comment">//获得被选中行的索引</span></span><br><span class="line">					<span class="keyword">if</span>(selectedRow!=-<span class="number">1</span>) &#123;<span class="comment">//判断是否被选中</span></span><br><span class="line">						<span class="comment">//修改表格模型中的指定值</span></span><br><span class="line">						tableModel.setValueAt(t1.getText(), selectedRow, <span class="number">0</span>);</span><br><span class="line">						tableModel.setValueAt(t2.getText(),selectedRow, <span class="number">0</span>);</span><br><span class="line">						tableModel.setValueAt(t3.getText(),selectedRow, <span class="number">0</span>);</span><br><span class="line">						tableModel.setValueAt(t4.getText(),selectedRow, <span class="number">0</span>);</span><br><span class="line">						l5.setText(<span class="string">&quot;修改成功&quot;</span>);</span><br><span class="line">						l5.setForeground(Color.darkGray);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				</span><br><span class="line">			&#125;);</span><br><span class="line">			b3=<span class="keyword">new</span> JButton(<span class="string">&quot;删除&quot;</span>);</span><br><span class="line">			b3.addActionListener(<span class="keyword">new</span> ActionListener() &#123;</span><br><span class="line"></span><br><span class="line">				<span class="meta">@Override</span></span><br><span class="line">				<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">					<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">					<span class="keyword">int</span> selectedRow=table.getSelectedRow();<span class="comment">//获得被选中的索引</span></span><br><span class="line">					<span class="keyword">if</span>(selectedRow!=-<span class="number">1</span>) &#123;<span class="comment">//判断是否被选中</span></span><br><span class="line">						<span class="comment">//从表格中删除指定行</span></span><br><span class="line">						tableModel.removeRow(selectedRow);</span><br><span class="line">						l5.setText(<span class="string">&quot;删除成功&quot;</span>);</span><br><span class="line">						l5.setForeground(Color.red);</span><br><span class="line">					&#125;</span><br><span class="line">					</span><br><span class="line">				&#125;</span><br><span class="line">				</span><br><span class="line">			&#125;);</span><br><span class="line">			p.add(l1);</span><br><span class="line">			p.add(t1);</span><br><span class="line">			p.add(l2);</span><br><span class="line">			p.add(t2);</span><br><span class="line">			p.add(l3);</span><br><span class="line">			p.add(t3);</span><br><span class="line">			p.add(l4);</span><br><span class="line">			p.add(t4);</span><br><span class="line">			p.add(l5);</span><br><span class="line">			p.add(b1);</span><br><span class="line">			p.add(b2);</span><br><span class="line">			p.add(b3);</span><br><span class="line">			p.setLayout(<span class="keyword">new</span> GridLayout(<span class="number">3</span>,<span class="number">2</span>));</span><br><span class="line">			f.add(p,BorderLayout.SOUTH);</span><br><span class="line">			f.add(gd,BorderLayout.CENTER);</span><br><span class="line">			f.pack();</span><br><span class="line">			f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">			f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> 默认方法创建();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/791896742af1ff02701adcfb36c82b15.png"></p>
<h2 id="JTree树"><a href="#JTree树" class="headerlink" title="JTree树"></a>JTree树</h2><p>1.<code>JTree</code>类的构造方法:<br>JTree()<br>JTree(TreeNode root)  //根据指定<code>根节点</code>创建树<br>JTree(TreeModel newModel) //根据指定树模型创建树<br>2.<code>DefaultMutableTreeNode</code>类实现了TreeNode接口，用来创建树的节点。<br>DefaultMutableTreeNode()<br>DefaultMutableTreeNode(Object userObject) //创建一个具有<code>指定标签</code>的节点<br>DefaultMutableTreeNode(Object userObject,boolean allowsChildren) //创建一个具有指定标签的节点，并且允许有子节点。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> Swing_tree;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.tree.DefaultMutableTreeNode;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 创建<span class="title">tree_2</span> </span>&#123;</span><br><span class="line">		JFrame f;</span><br><span class="line">		JTree tree;</span><br><span class="line">		DefaultMutableTreeNode top;<span class="comment">//资源管理器</span></span><br><span class="line">		<span class="comment">//我的公文包  我的电脑  收藏夹  Readme</span></span><br><span class="line">		DefaultMutableTreeNode node1_1,node1_2,node1_3,node1_4;</span><br><span class="line">		<span class="comment">//公司文件 私人文件  本机磁盘</span></span><br><span class="line">		DefaultMutableTreeNode node2_1,node2_2,node2_3,node2_4,node2_5;</span><br><span class="line">		<span class="comment">//网站列表</span></span><br><span class="line">		DefaultMutableTreeNode node3_1;</span><br><span class="line">		<span class="comment">//空</span></span><br><span class="line">		DefaultMutableTreeNode node4_1;</span><br><span class="line">		<span class="keyword">public</span> 创建tree_2() &#123;</span><br><span class="line">			f=<span class="keyword">new</span> JFrame(<span class="string">&quot;TreeDemo&quot;</span>);</span><br><span class="line">			top=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;资源管理器&quot;</span>);</span><br><span class="line">			</span><br><span class="line">			node1_1=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;我的公文包&quot;</span>);</span><br><span class="line">			node1_2=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;我的电脑&quot;</span>);</span><br><span class="line">			node1_3=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;收藏夹&quot;</span>);</span><br><span class="line">			node1_4=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;Readme&quot;</span>);</span><br><span class="line">			</span><br><span class="line">			node2_1=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;公司文件&quot;</span>);</span><br><span class="line">			node2_2=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;私人文件&quot;</span>);</span><br><span class="line">			node2_3=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;本机磁盘(C:)&quot;</span>);</span><br><span class="line">			node2_4=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;本机磁盘(D:)&quot;</span>);</span><br><span class="line">			node2_5=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;本机磁盘(E:)&quot;</span>);</span><br><span class="line">			</span><br><span class="line">			node3_1=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;网站列表&quot;</span>);</span><br><span class="line">			node4_1=<span class="keyword">new</span> DefaultMutableTreeNode(<span class="string">&quot;&lt;空&gt;&quot;</span>);</span><br><span class="line">			top.add(node1_1);</span><br><span class="line">			top.add(node1_2);</span><br><span class="line">			top.add(node1_3);</span><br><span class="line">			top.add(node1_4);</span><br><span class="line">			</span><br><span class="line">			node1_1.add(node2_1);</span><br><span class="line">			node1_1.add(node2_2);</span><br><span class="line">			node1_2.add(node2_3);</span><br><span class="line">			node1_2.add(node2_4);</span><br><span class="line">			node1_2.add(node2_5);</span><br><span class="line">			node1_3.add(node3_1);</span><br><span class="line">			</span><br><span class="line">			node3_1.add(node4_1);</span><br><span class="line">			tree=<span class="keyword">new</span> JTree(top);</span><br><span class="line">			f.add(tree);</span><br><span class="line">			f.setSize(<span class="number">400</span>,<span class="number">400</span>);</span><br><span class="line">			f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">			f.setResizable(<span class="keyword">true</span>);</span><br><span class="line">			f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">			<span class="keyword">new</span> 创建tree_2() ;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/0853fc6e23b16e61770838b0a3dfcd01.png"></p>
<h2 id="JMenu菜单"><a href="#JMenu菜单" class="headerlink" title="JMenu菜单"></a>JMenu菜单</h2><h3 id="下拉菜单"><a href="#下拉菜单" class="headerlink" title="下拉菜单"></a>下拉菜单</h3><p>菜单有如下三大组件：JMenuBar，JMenu，JMenuItem。<br>(1) 菜单栏(JMenuBar)：管理 菜单,不参与用户交互操作<br>(2)  菜单(JMenu)：整合管理菜单项（非叶结点）<br>(3)  菜单项(JMenuItem)：菜单系统中的基本组件（叶结点）<br>创建下拉菜单一般步骤<br>(1) 创建一个<code>JMenuBar</code>菜单栏对象，将其放置在JFrame窗口的顶部；<br>(2)  创建<code>JMenu</code>菜单对象，将其添加到<code>JMenuBar</code>菜单栏中;<br>(3)  创建<code>JMenuItem</code>菜单项，将其添加到<code>JMenu</code>菜单中。<br><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/934eb444e08650e25707967ce054fc84.png"></p>
<hr>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/bc769fbbdc58a62576aa9c3029940943.png"><br><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/5b00164a192996c8d6ad759f4e05921e.png"><br><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/c62bd3c3c8f14d05a179d4df64f921a3.png"><br><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/dd2dd1585d010fb274ecc1708b91882c.png"><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/c73619116a40401a2ca552128497c308.png"><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/0c6c1db5673153a535c5ad76a841ec5e.png"></p>
<hr>
<blockquote>
<p><code>JCheckboxMenuItem</code>类用于创建复选菜单项。</p>
</blockquote>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/fdf48913fb0040aa303702b3332c2f89.png"></p>
<blockquote>
<p><code>JRadioButtonMenuItem</code>类用于创建带有单选的菜单项。</p>
</blockquote>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/dd5663e6f48a732f3efc4695f33cd629.png"></p>
<h3 id="弹出菜单"><a href="#弹出菜单" class="headerlink" title="弹出菜单"></a>弹出菜单</h3><p>弹出式菜单：<code>JPopupMenu</code><br>(1) 构造方法： JPopupMenu ( );<br>(2)  常用方法：<br>① add( JMenuItem ) ：将指定菜单项添加到菜单末尾;<br>② <code>show</code>(Component , int , int ) ：<br>在组件调用者的坐标空间指定位置显示弹出菜单。<br>说明：弹出菜单默认不可见，需调用show()方法显示。<br>③<code> setVisible</code>( boolean ) ：设置弹出菜单的可见性。</p>
<h3 id="定制菜单"><a href="#定制菜单" class="headerlink" title="定制菜单"></a>定制菜单</h3><h4 id="使用分隔线"><a href="#使用分隔线" class="headerlink" title="使用分隔线"></a>使用分隔线</h4><p>(1) 作用：便于集中相似/相关的菜单项；<br>(2) JMenu类、JPopupMenu类提供添加分隔条方法<br><code>addSeparator()</code>：将新分隔符追加到菜单末尾；<br>Jmenu类还提供向指定索引位置插入分隔符方法：insertSeparator(int) 。<br>注意：索引位置从0开始。</p>
<h4 id="启动-禁用"><a href="#启动-禁用" class="headerlink" title="启动/禁用"></a>启动/禁用</h4><p>方法：<code>setEnable</code>(boolean);<br>① false：禁用菜单/菜单项；<br>② true：启用菜单/菜单项。<br>注意：禁用的菜单/菜单项呈灰色。</p>
<h4 id="设置快捷键"><a href="#设置快捷键" class="headerlink" title="设置快捷键"></a>设置快捷键</h4><p>(1) 作用：为菜单/菜单项设置快捷键；<br>(2) 设置方法<br>① <code>setMnemonic(int)</code>：参数为与键盘助记符对应的键值；<br>例：setMnemonic(KeyEvent.VK_F)，指定F为快捷键。<br>②<code> setMnemonic(char)</code> ：参数是键盘助记符对应的键值；<br>例：setMnemonic(‘F’)，指定F为快捷键。<br>(3) 说明：<br>①  如菜单名中含指定键盘快捷键，会为该键添加下划线；<br>②  快捷键<code>不区分大小写</code>。</p>
<h4 id="设置加速器"><a href="#设置加速器" class="headerlink" title="设置加速器"></a>设置加速器</h4><p>(1) 作用：不展开菜单 ，加速器可直接激活相应菜单项事件。<br>(2) 设置方法：<code>setaAccelerator(KeyStroke)</code><br>方法：<code>getKeyStroke</code>( int keyCode, int modifiers )<br> ① 作用： 获取<code>KeyStroke</code>对象(KeyStroke类中定义的静态方法)；<br> ②参数<code>keyCode</code>：键盘助记符对应的键值；<br> 由KeyEvent类中定义的VK_开头的静态常量指定。<br>③参数<code>modifiers</code>：多个参数，用|分隔<br>由InputEvent类中定义的静态常量指定。<br>例： CTRL_MASK、ALT_MASK、 SHIFT_MASK等 。<br>如：<br>getKeyStroke(KeyEvent.VK_<code>A</code>, InputEvent.<code>CTRL</code>_MASK | InputEvent.<code>ALT</code>_MASK));<br>//加速器按键为 CTRL+ALT+A</p>
<h3 id="笔记本实例"><a href="#笔记本实例" class="headerlink" title="笔记本实例"></a>笔记本实例</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> SWng_Menu;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.InputEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.KeyEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.MouseAdapter;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.MouseEvent;</span><br><span class="line"><span class="keyword">import</span> java.io.BufferedReader;</span><br><span class="line"><span class="keyword">import</span> java.io.BufferedWriter;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.FileReader;</span><br><span class="line"><span class="keyword">import</span> java.io.FileWriter;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 记事本作业版 <span class="keyword">implements</span> <span class="title">ActionListener</span> </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JMenuBar cd;</span><br><span class="line">	JMenu filecd,editcd,fontcd,lookcd,helpcd; <span class="comment">//五个主菜单  一级菜单</span></span><br><span class="line">	<span class="comment">//文件菜单</span></span><br><span class="line">	JMenuItem filemenuItem[]=<span class="keyword">new</span> JMenuItem[<span class="number">8</span>];<span class="comment">//文件下的8个副菜单 放进数组  二级菜单</span></span><br><span class="line">	String filemenuname[]= &#123;<span class="string">&quot;新建(N)&quot;</span>,<span class="string">&quot;新窗口(W)&quot;</span>,<span class="string">&quot;打开(O)&quot;</span>,<span class="string">&quot;保存(S)&quot;</span>,<span class="string">&quot;另存为(A)&quot;</span>,<span class="string">&quot;页面设置(U)&quot;</span>,<span class="string">&quot;打印(P)&quot;</span>,<span class="string">&quot;退出(X)&quot;</span>&#125;;<span class="comment">//文件</span></span><br><span class="line">	<span class="comment">//编辑菜单</span></span><br><span class="line">	JMenuItem editmenuItem[]=<span class="keyword">new</span> JMenuItem[<span class="number">13</span>];<span class="comment">//编辑下12个副菜单  二级菜单</span></span><br><span class="line">	String editmenuname[]= &#123;<span class="string">&quot;撤销(U)&quot;</span>,<span class="string">&quot;剪切(T)&quot;</span>,<span class="string">&quot;复制(C)&quot;</span>,<span class="string">&quot;粘贴(P)&quot;</span>,<span class="string">&quot;删除(L)&quot;</span>,<span class="string">&quot;使用Bing搜索&quot;</span>,<span class="string">&quot;查找(F)&quot;</span>,<span class="string">&quot;查找下一个(N)&quot;</span>,<span class="string">&quot;查找上一个(V)&quot;</span>,<span class="string">&quot;替换(R)&quot;</span>,<span class="string">&quot;转到(G)&quot;</span>,<span class="string">&quot;全选(V)&quot;</span>,<span class="string">&quot;时间/日期(D)&quot;</span>&#125;; <span class="comment">//编辑</span></span><br><span class="line">	<span class="comment">//格式</span></span><br><span class="line">	JMenu stylemenuItem[]=<span class="keyword">new</span> JMenu[<span class="number">2</span>];<span class="comment">// 二级菜单</span></span><br><span class="line">	String formatmenuname[]= &#123;<span class="string">&quot;自动换行(W)&quot;</span>,<span class="string">&quot;字体(F)&quot;</span>&#125;;<span class="comment">//格式</span></span><br><span class="line">	<span class="comment">//字体</span></span><br><span class="line">	ButtonGroup bg;</span><br><span class="line">		JRadioButtonMenuItem fontbody[]=<span class="keyword">new</span> JRadioButtonMenuItem[<span class="number">4</span>]; <span class="comment">//单选字体   三级菜单</span></span><br><span class="line">		String fontbodyname[]= &#123;<span class="string">&quot;黑体&quot;</span>,<span class="string">&quot;宋体&quot;</span>,<span class="string">&quot;楷体&quot;</span>,<span class="string">&quot;隶书&quot;</span>&#125;; <span class="comment">//字体</span></span><br><span class="line">	<span class="comment">//查看</span></span><br><span class="line">	JMenuItem lookmenuItem[]=<span class="keyword">new</span> JMenuItem[<span class="number">2</span>];</span><br><span class="line">	String lookname[]= &#123;<span class="string">&quot;缩放(Z)&quot;</span>,<span class="string">&quot;状态栏(S)&quot;</span>&#125;;</span><br><span class="line">	<span class="comment">//帮助</span></span><br><span class="line">	JMenuItem helpmenuItem[]=<span class="keyword">new</span> JMenuItem[<span class="number">3</span>];</span><br><span class="line">	String helpname[]= &#123;<span class="string">&quot;查看帮助(H)&quot;</span>,<span class="string">&quot;发送反馈(F)&quot;</span>,<span class="string">&quot;关于记事本&quot;</span>&#125;;</span><br><span class="line">	<span class="comment">//工具栏</span></span><br><span class="line">	JToolBar tb;</span><br><span class="line">	JButton Toolbu[]=<span class="keyword">new</span> JButton[<span class="number">7</span>];</span><br><span class="line">	String Toolname[]= &#123;<span class="string">&quot;新建&quot;</span>,<span class="string">&quot;打开&quot;</span>,<span class="string">&quot;保存&quot;</span>,<span class="string">&quot;剪切&quot;</span>,<span class="string">&quot;复制&quot;</span>,<span class="string">&quot;粘贴&quot;</span>,<span class="string">&quot;字体&quot;</span>&#125;;</span><br><span class="line">	<span class="comment">//文本域</span></span><br><span class="line">		JTextArea t;</span><br><span class="line">		JScrollPane gd;</span><br><span class="line">	<span class="comment">//弹出菜单</span></span><br><span class="line">		JPopupMenu tm;<span class="comment">//弹出菜单</span></span><br><span class="line">		JMenuItem PopumenuItem[]=<span class="keyword">new</span> JMenuItem[<span class="number">4</span>];<span class="comment">//弹出</span></span><br><span class="line">		String popname[]= &#123;<span class="string">&quot;全选&quot;</span>,<span class="string">&quot;复制&quot;</span>,<span class="string">&quot;粘贴&quot;</span>,<span class="string">&quot;剪切&quot;</span>&#125;;  <span class="comment">//弹出窗口</span></span><br><span class="line">	<span class="keyword">public</span> 记事本作业版() &#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;新建文本文档-记事本&quot;</span>);</span><br><span class="line">		f.setIconImage(<span class="keyword">new</span> ImageIcon(<span class="string">&quot;Image/记事本.png&quot;</span>).getImage());</span><br><span class="line">		cd=<span class="keyword">new</span> JMenuBar();<span class="comment">//工具条</span></span><br><span class="line">		<span class="comment">//五个主菜单</span></span><br><span class="line">		filecd=<span class="keyword">new</span> JMenu(<span class="string">&quot;文件(F)&quot;</span>);</span><br><span class="line">		filecd.setMnemonic(<span class="string">&#x27;F&#x27;</span>);</span><br><span class="line">		editcd=<span class="keyword">new</span> JMenu(<span class="string">&quot;编辑(E)&quot;</span>);</span><br><span class="line">		editcd.setMnemonic(<span class="string">&#x27;E&#x27;</span>);</span><br><span class="line">		fontcd=<span class="keyword">new</span> JMenu(<span class="string">&quot;格式(O)&quot;</span>);</span><br><span class="line">		fontcd.setMnemonic(<span class="string">&#x27;O&#x27;</span>);</span><br><span class="line">		lookcd=<span class="keyword">new</span> JMenu(<span class="string">&quot;查看(V)&quot;</span>);</span><br><span class="line">		lookcd.setMnemonic(<span class="string">&#x27;V&#x27;</span>);</span><br><span class="line">		helpcd=<span class="keyword">new</span> JMenu(<span class="string">&quot;帮助(H)&quot;</span>);</span><br><span class="line">		helpcd.setMnemonic(<span class="string">&#x27;H&#x27;</span>);</span><br><span class="line">		cd.add(filecd);</span><br><span class="line">		cd.add(editcd);</span><br><span class="line">		cd.add(fontcd);</span><br><span class="line">		cd.add(lookcd);</span><br><span class="line">		cd.add(helpcd);</span><br><span class="line">		f.setJMenuBar(cd);</span><br><span class="line">		<span class="comment">//文件添加</span></span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;filemenuname.length;i++) &#123;</span><br><span class="line">					filemenuItem[i]=<span class="keyword">new</span> JMenuItem(filemenuname[i]);<span class="comment">//每一个菜单项加名字</span></span><br><span class="line">					<span class="keyword">if</span>(i&gt;<span class="number">0</span>&amp;&amp;i%<span class="number">2</span>==<span class="number">0</span>)</span><br><span class="line">						filecd.addSeparator();</span><br><span class="line">					filecd.add(filemenuItem[i]);<span class="comment">//都加到文件主菜单上</span></span><br><span class="line">					filemenuItem[i].addActionListener(<span class="keyword">this</span>);<span class="comment">//都加监听</span></span><br><span class="line">				&#125;</span><br><span class="line">				filemenuItem[<span class="number">1</span>].setEnabled(<span class="keyword">false</span>);	<span class="comment">//新窗口</span></span><br><span class="line">				filemenuItem[<span class="number">4</span>].setEnabled(<span class="keyword">false</span>);	<span class="comment">//另存为</span></span><br><span class="line">				filemenuItem[<span class="number">5</span>].setEnabled(<span class="keyword">false</span>);  <span class="comment">//页面设置</span></span><br><span class="line">				filemenuItem[<span class="number">6</span>].setEnabled(<span class="keyword">false</span>);  <span class="comment">//打印</span></span><br><span class="line">				filemenuItem[<span class="number">0</span>].setMnemonic(<span class="string">&#x27;N&#x27;</span>);</span><br><span class="line">				filemenuItem[<span class="number">2</span>].setMnemonic(<span class="string">&#x27;O&#x27;</span>);</span><br><span class="line">				filemenuItem[<span class="number">3</span>].setMnemonic(<span class="string">&#x27;S&#x27;</span>);</span><br><span class="line">				filemenuItem[<span class="number">0</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));</span><br><span class="line">				filemenuItem[<span class="number">2</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));</span><br><span class="line">				filemenuItem[<span class="number">3</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));</span><br><span class="line">				filemenuItem[<span class="number">7</span>].setMnemonic(<span class="string">&#x27;X&#x27;</span>);</span><br><span class="line"></span><br><span class="line">				<span class="comment">//编辑添加</span></span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;editmenuname.length;i++) &#123;</span><br><span class="line">					editmenuItem[i]=<span class="keyword">new</span> JMenuItem(editmenuname[i]);<span class="comment">//加名字</span></span><br><span class="line">					<span class="keyword">if</span>(i&gt;<span class="number">0</span>&amp;&amp;i%<span class="number">2</span>==<span class="number">0</span>)</span><br><span class="line">						editcd.addSeparator();</span><br><span class="line">					editcd.add(editmenuItem[i]);</span><br><span class="line">					editmenuItem[i].addActionListener(<span class="keyword">this</span>);</span><br><span class="line">			&#125;</span><br><span class="line">				editmenuItem[<span class="number">0</span>].setEnabled(<span class="keyword">false</span>);</span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;<span class="number">11</span>;i++) &#123;</span><br><span class="line">					editmenuItem[i].setEnabled(<span class="keyword">false</span>);</span><br><span class="line">				&#125;</span><br><span class="line">				editmenuItem[<span class="number">1</span>].setMnemonic(<span class="string">&#x27;T&#x27;</span>); <span class="comment">//剪切</span></span><br><span class="line">				editmenuItem[<span class="number">1</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));</span><br><span class="line">				editmenuItem[<span class="number">2</span>].setMnemonic(<span class="string">&#x27;C&#x27;</span>); <span class="comment">//复制</span></span><br><span class="line">				editmenuItem[<span class="number">2</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));</span><br><span class="line">				editmenuItem[<span class="number">3</span>].setMnemonic(<span class="string">&#x27;V&#x27;</span>); <span class="comment">//粘贴</span></span><br><span class="line">				editmenuItem[<span class="number">3</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));</span><br><span class="line">				editmenuItem[<span class="number">11</span>].setMnemonic(<span class="string">&#x27;A&#x27;</span>); <span class="comment">//全选</span></span><br><span class="line">				editmenuItem[<span class="number">11</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));</span><br><span class="line">				editmenuItem[<span class="number">12</span>].setMnemonic(<span class="string">&#x27;D&#x27;</span>);  <span class="comment">//时间</span></span><br><span class="line">				editmenuItem[<span class="number">12</span>].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,InputEvent.CTRL_MASK));</span><br><span class="line">			<span class="comment">//格式添加</span></span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;formatmenuname.length;i++) &#123;</span><br><span class="line">					 stylemenuItem[i]=<span class="keyword">new</span> JMenu(formatmenuname[i]);</span><br><span class="line">					 <span class="keyword">if</span>(i&gt;<span class="number">0</span>)</span><br><span class="line">						 fontcd.addSeparator();</span><br><span class="line">					 fontcd.add(stylemenuItem[i]);</span><br><span class="line">				&#125;</span><br><span class="line">				stylemenuItem[<span class="number">0</span>].setMnemonic(<span class="string">&#x27;W&#x27;</span>);</span><br><span class="line">				stylemenuItem[<span class="number">0</span>].setEnabled(<span class="keyword">false</span>);</span><br><span class="line">				stylemenuItem[<span class="number">1</span>].setMnemonic(<span class="string">&#x27;F&#x27;</span>);</span><br><span class="line">				<span class="comment">//加字体</span></span><br><span class="line">				bg=<span class="keyword">new</span> ButtonGroup();</span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;fontbodyname.length;i++) &#123;</span><br><span class="line">					fontbody[i]=<span class="keyword">new</span> JRadioButtonMenuItem(fontbodyname[i]);<span class="comment">//4个字体 加上名字</span></span><br><span class="line">					bg.add(fontbody[i]);<span class="comment">//添加到按键组 实现单选</span></span><br><span class="line">					stylemenuItem[<span class="number">1</span>].add(fontbody[i]);<span class="comment">//把4个字体文字添加到字体副菜单</span></span><br><span class="line">					fontbody[i].addActionListener(<span class="keyword">this</span>);</span><br><span class="line">				&#125;</span><br><span class="line">			<span class="comment">//查看添加</span></span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt; lookname.length;i++) &#123;</span><br><span class="line">					lookmenuItem[i]=<span class="keyword">new</span> JMenuItem(lookname[i]);</span><br><span class="line">					lookmenuItem[i].setEnabled(<span class="keyword">false</span>);</span><br><span class="line">					<span class="keyword">if</span>(i==<span class="number">1</span>)</span><br><span class="line">					lookcd.addSeparator();</span><br><span class="line">					lookcd.add(lookmenuItem[i]);</span><br><span class="line">				&#125;</span><br><span class="line">				lookmenuItem[<span class="number">0</span>].setMnemonic(<span class="string">&#x27;Z&#x27;</span>);</span><br><span class="line">				lookmenuItem[<span class="number">1</span>].setMnemonic(<span class="string">&#x27;S&#x27;</span>);</span><br><span class="line">			<span class="comment">//帮助添加</span></span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;helpname.length;i++) &#123;</span><br><span class="line">					helpmenuItem[i]=<span class="keyword">new</span> JMenuItem(helpname[i]);</span><br><span class="line">					<span class="keyword">if</span>(i==<span class="number">1</span>||i==<span class="number">2</span>)</span><br><span class="line">						helpcd.addSeparator();</span><br><span class="line">					<span class="keyword">if</span>(i==<span class="number">0</span>||i==<span class="number">1</span>)</span><br><span class="line">						helpmenuItem[i].setEnabled(<span class="keyword">false</span>);</span><br><span class="line">					helpcd.add(helpmenuItem[i]);</span><br><span class="line">				&#125;</span><br><span class="line">				helpmenuItem[<span class="number">2</span>].addActionListener(<span class="keyword">new</span> CreatNewWindos());<span class="comment">//关于记事本</span></span><br><span class="line">				<span class="comment">//文本域设置</span></span><br><span class="line">				t=<span class="keyword">new</span> JTextArea();</span><br><span class="line">				t.setFont(<span class="keyword">new</span> Font(<span class="string">&quot;微软雅黑&quot;</span>,Font.PLAIN,<span class="number">20</span>));</span><br><span class="line">				gd=<span class="keyword">new</span> JScrollPane(t);  <span class="comment">//文本域加滚动</span></span><br><span class="line">				f.add(gd,BorderLayout.CENTER);</span><br><span class="line">				<span class="comment">//弹出菜单</span></span><br><span class="line">				tm=<span class="keyword">new</span> JPopupMenu();</span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;popname.length;i++) &#123;</span><br><span class="line">					PopumenuItem[i]=<span class="keyword">new</span> JMenuItem(popname[i]);<span class="comment">//名字</span></span><br><span class="line">					PopumenuItem[i].addActionListener(<span class="keyword">this</span>);</span><br><span class="line">					<span class="keyword">if</span>(i&gt;<span class="number">0</span>)</span><br><span class="line">					tm.addSeparator(); <span class="comment">//加分隔符</span></span><br><span class="line">					tm.add(PopumenuItem[i]);<span class="comment">//加到弹出菜单上</span></span><br><span class="line">					</span><br><span class="line">				&#125;</span><br><span class="line">				t.addMouseListener(<span class="keyword">new</span> MouseAdapter() &#123;</span><br><span class="line">					<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">mouseReleased</span><span class="params">(MouseEvent e)</span> </span>&#123;</span><br><span class="line">						<span class="keyword">if</span>(e.isPopupTrigger())</span><br><span class="line">							tm.show(e.getComponent(),e.getX(),e.getY());</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;);	</span><br><span class="line">				String Tooltext[]= &#123;<span class="string">&quot;新建&quot;</span>,<span class="string">&quot;打开&quot;</span>,<span class="string">&quot;保存&quot;</span>,<span class="string">&quot;剪切&quot;</span>,<span class="string">&quot;复制&quot;</span>,<span class="string">&quot;粘贴&quot;</span>,<span class="string">&quot;字体&quot;</span>&#125;;</span><br><span class="line">				<span class="comment">//工具栏设置</span></span><br><span class="line">				tb=<span class="keyword">new</span> JToolBar();</span><br><span class="line">				<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;Toolname.length;i++) &#123;</span><br><span class="line">					Toolbu[i]=<span class="keyword">new</span> JButton(Toolname[i]);<span class="comment">//按键加图片</span></span><br><span class="line">					Toolbu[i].setToolTipText(Tooltext[i]);<span class="comment">//鼠标指向的时候</span></span><br><span class="line">					Toolbu[i].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));<span class="comment">//添加一个手的样子 提示用户可以点击</span></span><br><span class="line">					<span class="keyword">if</span>(i==<span class="number">3</span>||i==<span class="number">6</span>)</span><br><span class="line">						tb.addSeparator(<span class="keyword">new</span> Dimension(<span class="number">20</span>,<span class="number">0</span>));<span class="comment">//分隔符</span></span><br><span class="line">					tb.add(Toolbu[i]);<span class="comment">//加到工具栏</span></span><br><span class="line">					Toolbu[i].addActionListener(<span class="keyword">this</span>);</span><br><span class="line">				&#125;</span><br><span class="line">				</span><br><span class="line">				f.add(tb,BorderLayout.NORTH);<span class="comment">//工具栏设置到北面</span></span><br><span class="line">				tb.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		f.setSize(<span class="number">800</span>,<span class="number">600</span>);</span><br><span class="line">		f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setResizable(<span class="keyword">true</span>);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//关于记事本的弹出窗口</span></span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">CreatNewWindos</span> <span class="keyword">implements</span> <span class="title">ActionListener</span></span>&#123;</span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">			<span class="comment">// TODO 自动生成的方法存</span></span><br><span class="line">			JFrame F=<span class="keyword">new</span> JFrame(<span class="string">&quot;关于记事本&quot;</span>);</span><br><span class="line">			JLabel l=<span class="keyword">new</span> JLabel(<span class="string">&quot;开发者:18软件工程三班李振业&quot;</span>+<span class="string">&quot;  &quot;</span>+<span class="string">&quot;2019.10.26&quot;</span>);</span><br><span class="line">			l.setFont(<span class="keyword">new</span> Font(<span class="string">&quot;楷体&quot;</span>,Font.BOLD,<span class="number">20</span>));</span><br><span class="line">			l.setForeground(Color.GRAY);</span><br><span class="line">				F.add(l);</span><br><span class="line">				F.setSize(<span class="number">500</span>,<span class="number">300</span>);</span><br><span class="line">				F.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">				F.setVisible(<span class="keyword">true</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> 记事本作业版();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		 Object event=e.getSource(); <span class="comment">//取按键变量</span></span><br><span class="line">		 <span class="comment">/***新建***/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 <span class="keyword">if</span>(event==filemenuItem[<span class="number">0</span>]||event==Toolbu[<span class="number">0</span>]) &#123;</span><br><span class="line">			 t.setText(<span class="string">&quot;&quot;</span>); <span class="comment">//清空</span></span><br><span class="line">			 f.setTitle(<span class="string">&quot;新建文本文档-记事本&quot;</span>);</span><br><span class="line">		 &#125;</span><br><span class="line"><span class="comment">/***退出***/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 <span class="keyword">if</span>(event==filemenuItem[<span class="number">7</span>]) &#123;</span><br><span class="line">			 System.exit(<span class="number">0</span>);</span><br><span class="line">		 &#125;</span><br><span class="line"><span class="comment">/***打开***/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 <span class="keyword">if</span>(event==filemenuItem[<span class="number">2</span>]||event==Toolbu[<span class="number">1</span>]) &#123;</span><br><span class="line">			 JFileChooser  filechoose=<span class="keyword">new</span> JFileChooser();<span class="comment">//弹出一个窗口</span></span><br><span class="line">			 filechoose.setDialogTitle(<span class="string">&quot;文件打开&quot;</span>);<span class="comment">//显示弹开窗口的标题</span></span><br><span class="line">			 filechoose.showOpenDialog(<span class="keyword">null</span>);<span class="comment">//ShowOpen 改变按钮的显示为打开</span></span><br><span class="line">			 filechoose.setVisible(<span class="keyword">true</span>);</span><br><span class="line">			 String open_filepath=filechoose.getSelectedFile().getAbsolutePath();</span><br><span class="line">				<span class="comment">//获取此文件的绝对路径</span></span><br><span class="line">				</span><br><span class="line">				<span class="comment">//读取文件</span></span><br><span class="line">			 <span class="keyword">try</span> &#123;</span><br><span class="line">				File open_file=<span class="keyword">new</span> File(open_filepath);<span class="comment">//根据路径创建文件</span></span><br><span class="line">				FileReader fr=<span class="keyword">new</span> FileReader(open_file); <span class="comment">//读取流</span></span><br><span class="line">				BufferedReader br=<span class="keyword">new</span> BufferedReader(fr); <span class="comment">//缓存流</span></span><br><span class="line">				</span><br><span class="line">				<span class="keyword">char</span> byt[]=<span class="keyword">new</span> <span class="keyword">char</span>[(<span class="keyword">int</span>)open_file.length()]; <span class="comment">//转为字节流</span></span><br><span class="line">				<span class="keyword">int</span> content=br.read(byt);  <span class="comment">//读取内容</span></span><br><span class="line">				f.setTitle(open_file.getName());  <span class="comment">//窗口改变名字</span></span><br><span class="line">				t.append(<span class="keyword">new</span> String(byt,<span class="number">0</span>,content));<span class="comment">//文本域append追加显示文字</span></span><br><span class="line">				fr.close();<span class="comment">//关闭流</span></span><br><span class="line">			 &#125;<span class="keyword">catch</span>(Exception e1) &#123;</span><br><span class="line">				 System.out.println(<span class="string">&quot;没有打开文件&quot;</span>);</span><br><span class="line">			 &#125;</span><br><span class="line">		 &#125;</span><br><span class="line">		 <span class="comment">/***保存**/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 <span class="keyword">if</span>(event==filemenuItem[<span class="number">3</span>]||event==Toolbu[<span class="number">2</span>]) &#123;</span><br><span class="line"></span><br><span class="line">			  <span class="keyword">try</span> &#123;</span><br><span class="line">				 JFileChooser  filechoose=<span class="keyword">new</span> JFileChooser();<span class="comment">//弹出一个窗口</span></span><br><span class="line">				 filechoose.setDialogTitle(<span class="string">&quot;另存为&quot;</span>);<span class="comment">//显示弹开窗口的标题</span></span><br><span class="line">				 filechoose.showSaveDialog(<span class="keyword">null</span>);  <span class="comment">//ShowSava 改变按钮的显示为保存</span></span><br><span class="line">				 filechoose.setVisible(<span class="keyword">true</span>);</span><br><span class="line">				 String save_filepath=filechoose.getSelectedFile().getAbsolutePath();</span><br><span class="line">					<span class="comment">//获取此文件的绝对路径</span></span><br><span class="line">					  </span><br><span class="line">					  File save_file=<span class="keyword">new</span> File(save_filepath);</span><br><span class="line">					  FileWriter fw=<span class="keyword">new</span> FileWriter(save_file);<span class="comment">//写入流</span></span><br><span class="line">					  BufferedWriter bw=<span class="keyword">new</span> BufferedWriter(fw); <span class="comment">//缓存流</span></span><br><span class="line">					  bw.write(t.getText());<span class="comment">//文本域的内容写入文件</span></span><br><span class="line">					  bw.close();</span><br><span class="line">					  fw.close();</span><br><span class="line">			&#125; <span class="keyword">catch</span> (IOException e1) &#123;</span><br><span class="line">				<span class="comment">// TODO 自动生成的 catch 块</span></span><br><span class="line">				e1.printStackTrace();</span><br><span class="line">			&#125;</span><br><span class="line">		 &#125;</span><br><span class="line">		 </span><br><span class="line"><span class="comment">/***全选**/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 <span class="keyword">if</span>(event==editmenuItem[<span class="number">11</span>]||event==PopumenuItem[<span class="number">0</span>]) &#123;</span><br><span class="line">			 t.selectAll();<span class="comment">//选定指定的开始和结束位置之间的文本。</span></span><br><span class="line">		 &#125;</span><br><span class="line"> <span class="comment">/***时间日期**/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 	<span class="keyword">if</span>(event==editmenuItem[<span class="number">12</span>]) &#123;</span><br><span class="line">		 		Date dt=<span class="keyword">new</span> Date();</span><br><span class="line">				SimpleDateFormat time=<span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyy-MM-dd HH:mm:ss E&quot;</span>);</span><br><span class="line">				t.append(time.format(dt));<span class="comment">//文本域append追加显示文字</span></span><br><span class="line">		 	&#125;</span><br><span class="line">		 	<span class="comment">/***字体**/</span><span class="comment">//	//////////////////////////	</span></span><br><span class="line">		 	<span class="comment">//黑体</span></span><br><span class="line">		 	<span class="keyword">if</span>(event==fontbody[<span class="number">0</span>]) &#123;</span><br><span class="line">		 		 Font old_font=t.getFont();</span><br><span class="line">		 		 Font new_font=<span class="keyword">new</span> Font(<span class="string">&quot;黑体&quot;</span>,old_font.getStyle(),old_font.getSize());</span><br><span class="line">		 		 t.setFont(new_font);</span><br><span class="line">		 	&#125;</span><br><span class="line">		 	<span class="comment">//宋体</span></span><br><span class="line">		 	<span class="keyword">if</span>(event==fontbody[<span class="number">1</span>]) &#123;</span><br><span class="line">		 		Font old_font=t.getFont();</span><br><span class="line">		 		Font new_font=<span class="keyword">new</span> Font(<span class="string">&quot;宋体&quot;</span>,old_font.getStyle(),old_font.getSize());</span><br><span class="line">		 		t.setFont(new_font);</span><br><span class="line">		 	&#125;</span><br><span class="line">		 	<span class="comment">//楷体</span></span><br><span class="line">		 	<span class="keyword">if</span>(event==fontbody[<span class="number">2</span>]) &#123;</span><br><span class="line">		 		Font old_font=t.getFont();</span><br><span class="line">		 		Font new_font=<span class="keyword">new</span> Font(<span class="string">&quot;楷体&quot;</span>,old_font.getStyle(),old_font.getSize());</span><br><span class="line">		 		t.setFont(new_font);</span><br><span class="line">		 	&#125;</span><br><span class="line">		 	<span class="comment">//隶书</span></span><br><span class="line">		 	<span class="keyword">if</span>(event==fontbody[<span class="number">3</span>]) &#123;</span><br><span class="line">		 		Font old_font=t.getFont();</span><br><span class="line">		 		Font new_font=<span class="keyword">new</span> Font(<span class="string">&quot;隶书&quot;</span>,old_font.getStyle(),old_font.getSize());</span><br><span class="line">		 		t.setFont(new_font);</span><br><span class="line">		 	&#125;</span><br><span class="line">		 	<span class="comment">//字体窗口</span></span><br><span class="line">		 	<span class="keyword">if</span>(event==Toolbu[<span class="number">6</span>]) &#123;</span><br><span class="line">		 		<span class="keyword">new</span> 字体窗口();</span><br><span class="line">		 	&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/4897c94084ba1608179813a5c5aab115.png"></p>
<h2 id="自动弹窗"><a href="#自动弹窗" class="headerlink" title="自动弹窗"></a>自动弹窗</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> 自动弹窗;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="comment">/*1.JOptionPane.showMessageDialog(组件, “信息的弹框”);</span></span><br><span class="line"><span class="comment"> * showMessageDialog(Component parentComponent, Object message, String title, int messageType)</span></span><br><span class="line"><span class="comment"> *2. JOptionPane.showMessageDialog(null, “信息的弹框”, “窗口名字”, 默认或者风格);</span></span><br><span class="line"><span class="comment"> *默认或者风格种类: </span></span><br><span class="line"><span class="comment"> *1.参数为1  默认</span></span><br><span class="line"><span class="comment"> *2.参数为-1  无提示类型(图标)</span></span><br><span class="line"><span class="comment"> *3.参数为0   含义为错误(出现错误图标)</span></span><br><span class="line"><span class="comment"> *4.参数为2   含义为警告(出现感叹号图标)</span></span><br><span class="line"><span class="comment"> *5.参数为3  含义为问题(出现问题图标)</span></span><br><span class="line"><span class="comment"> *3.showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)</span></span><br><span class="line"><span class="comment"> *	定义图标，加载图片的图标，可以帮助用户识别正在显示消息的种类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">showMessageDialog</span> <span class="keyword">implements</span> <span class="title">ActionListener</span> </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JPanel p;</span><br><span class="line">	ImageIcon img=<span class="keyword">new</span> ImageIcon(<span class="string">&quot;Image/qq图标.jpg&quot;</span>);</span><br><span class="line">	JButton b1,b2,b3,b4,b5,b6;<span class="comment">//默认  不带图标 警告 错误 问题 图标</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">showMessageDialog</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;测试窗口&quot;</span>);</span><br><span class="line">		b1=<span class="keyword">new</span> JButton(<span class="string">&quot;默认&quot;</span>);</span><br><span class="line">		b1.addActionListener(<span class="keyword">this</span>);</span><br><span class="line">		b2=<span class="keyword">new</span> JButton(<span class="string">&quot;无图标&quot;</span>);</span><br><span class="line">		b2.addActionListener(<span class="keyword">this</span>);</span><br><span class="line">		b3=<span class="keyword">new</span> JButton(<span class="string">&quot;错误&quot;</span>);</span><br><span class="line">		b3.addActionListener(<span class="keyword">this</span>);</span><br><span class="line">		b4=<span class="keyword">new</span> JButton(<span class="string">&quot;警告&quot;</span>);</span><br><span class="line">		b4.addActionListener(<span class="keyword">this</span>);</span><br><span class="line">		b5=<span class="keyword">new</span> JButton(<span class="string">&quot;问题&quot;</span>);</span><br><span class="line">		b5.addActionListener(<span class="keyword">this</span>);</span><br><span class="line">		b6=<span class="keyword">new</span> JButton(<span class="string">&quot;图标&quot;</span>);</span><br><span class="line">		b6.addActionListener(<span class="keyword">this</span>);</span><br><span class="line">		p=<span class="keyword">new</span> JPanel(<span class="keyword">new</span> GridLayout(<span class="number">2</span>,<span class="number">3</span>));</span><br><span class="line">		p.add(b1);</span><br><span class="line">		p.add(b2);</span><br><span class="line">		p.add(b3);</span><br><span class="line">		p.add(b4);</span><br><span class="line">		p.add(b5);</span><br><span class="line">		p.add(b6);</span><br><span class="line">		f.add(p);</span><br><span class="line">		f.pack();</span><br><span class="line">		f.setLocationRelativeTo(<span class="keyword">null</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">	    <span class="keyword">new</span> 	showMessageDialog();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		String event=e.getActionCommand();</span><br><span class="line">		<span class="keyword">if</span>(event.equals(<span class="string">&quot;默认&quot;</span>)) &#123;</span><br><span class="line">			JOptionPane.showMessageDialog(p, <span class="string">&quot;默认&quot;</span>,<span class="string">&quot;提示窗口&quot;</span>,<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(event.equals(<span class="string">&quot;无图标&quot;</span>)) &#123;</span><br><span class="line">			JOptionPane.showMessageDialog(p,<span class="string">&quot;无图标&quot;</span>,<span class="string">&quot;提示窗口&quot;</span>,-<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(event.equals(<span class="string">&quot;错误&quot;</span>)) &#123;</span><br><span class="line">			JOptionPane.showMessageDialog(p,<span class="string">&quot;错误&quot;</span>,<span class="string">&quot;提示窗口&quot;</span>,<span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(event.equals(<span class="string">&quot;警告&quot;</span>)) &#123;</span><br><span class="line">			JOptionPane.showMessageDialog(p, <span class="string">&quot;警告&quot;</span>,<span class="string">&quot;提示窗口&quot;</span>,<span class="number">2</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(event.equals(<span class="string">&quot;问题&quot;</span>)) &#123;</span><br><span class="line">			JOptionPane.showMessageDialog(p, <span class="string">&quot;问题&quot;</span>,<span class="string">&quot;提示窗口&quot;</span>,<span class="number">3</span>);</span><br><span class="line">		&#125;<span class="keyword">if</span>(event.equals(<span class="string">&quot;图标&quot;</span>)) &#123;</span><br><span class="line">			JOptionPane.showMessageDialog(p, <span class="string">&quot;问题&quot;</span>,<span class="string">&quot;提示窗口&quot;</span>,<span class="number">3</span>,img);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="文件选择器"><a href="#文件选择器" class="headerlink" title="文件选择器"></a>文件选择器</h2><h3 id="文件选择对话框"><a href="#文件选择对话框" class="headerlink" title="文件选择对话框"></a>文件选择对话框</h3><p><code>JFileChooser</code>类提供了一个供用户<code>选择文件</code>的对话框。利用该类创建文件选择对话框以及获取用户选择文件的基本步骤如下：<br>（1）创建一个JFileChooser类的对象。<br>（2）默认情况下每次只能选择一个文件，如果希望允许同时选择多个文件，可以通过调用方法<code>setMultiSelectionEnabled(boolean b)</code>设置，将入口参数设为true即表示允许多选。<br>（3）默认情况下只允许选择文件，如果希望允许选择文件夹，可以通过调用方法<code>setFileSelection- Mode(int mode)</code>设置，入口参数可选的静态常量有<code>FILES_ONLY</code>（只允许选择文件）、<code>DIRECTORIES_ ONLY</code>（只允许选择路径）和<code>FILES_AND_DIRECTORIES</code>（均可选择）。<br>（4）如果只希望在对话框中列出指定类型的文件，可以调用<code>setFileFilter(FileFilter filter)</code>方法设置文件过滤器。<br>（5）设置完成后调用<code>showOpenDialog(Component parent)</code>方法显示对话框，该方法将返回一个int型值，用来判断用户是否选择了文件或路径。<br>（6）如果用户选择了文件或路径，可以通过方法<code>getSelectedFile()</code>或<code>getSelectedFiles()</code>获得，方法getSelectedFile()返回的是File对象，方法getSelectedFiles()返回的是File型数组。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> SWng_Menu;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 文件选择器 </span>&#123;</span><br><span class="line">	JFrame f;</span><br><span class="line">	JLabel l;</span><br><span class="line">	JButton b;</span><br><span class="line">	JTextField t;</span><br><span class="line">	JPanel p;</span><br><span class="line">	<span class="keyword">public</span> 文件选择器() &#123;</span><br><span class="line">		f=<span class="keyword">new</span> JFrame(<span class="string">&quot;文件选择器&quot;</span>);</span><br><span class="line">		l=<span class="keyword">new</span> JLabel(<span class="string">&quot;文件:&quot;</span>);</span><br><span class="line">		b=<span class="keyword">new</span> JButton(<span class="string">&quot;打开&quot;</span>);</span><br><span class="line">		b.addActionListener(<span class="keyword">new</span> ActionListener() &#123;</span><br><span class="line">			<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(ActionEvent e)</span> </span>&#123;</span><br><span class="line">				<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">					JFileChooser wjchoose=<span class="keyword">new</span> JFileChooser();<span class="comment">//创建文件选择器</span></span><br><span class="line">					<span class="comment">//显示文件选择对话框</span></span><br><span class="line">					<span class="keyword">int</span> i=wjchoose.showOpenDialog(f);</span><br><span class="line">					<span class="comment">//判断用户单击的是否为“打开”按钮</span></span><br><span class="line">					<span class="keyword">if</span>(i==JFileChooser.APPROVE_OPTION) &#123;</span><br><span class="line">						<span class="comment">//获取选中的文件对象</span></span><br><span class="line">						File selected=wjchoose.getSelectedFile();</span><br><span class="line">						<span class="comment">//显示选中文件的名称</span></span><br><span class="line">						t.setText(selected.getName());</span><br><span class="line">						<span class="comment">//t.setText(selected.getAbsolutePath());</span></span><br><span class="line">					&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;);</span><br><span class="line">		t=<span class="keyword">new</span> JTextField(<span class="number">20</span>);</span><br><span class="line">		p=<span class="keyword">new</span> JPanel();</span><br><span class="line">		p.add(l);</span><br><span class="line">		p.add(t);</span><br><span class="line">		p.add(b);</span><br><span class="line">		f.add(p);</span><br><span class="line">		f.setSize(<span class="number">400</span>,<span class="number">100</span>);</span><br><span class="line">		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">		f.setLocation(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line">		f.setResizable(<span class="keyword">false</span>);</span><br><span class="line">		f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> 文件选择器();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">![](https:<span class="comment">//cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/aa5cd174f9d7e46ed9e86f8d280df984.png)</span></span><br></pre></td></tr></table></figure>
<h3 id="文件过滤器"><a href="#文件过滤器" class="headerlink" title="文件过滤器"></a>文件过滤器</h3><p>如果只希望在对话框中列出指定类型的文件，可以调用<code>setFileFilter(FileFilter filter)</code>方法设置文件过滤器。</p>
<p>javax.swing.filechooser.FileFilter类是一个<code>抽象类</code>，该类的具体定义如下：<br>public abstract class FileFilter {<br>    public abstract boolean <code>accept</code>(File f);<br>    public abstract String <code>getDescription</code>();<br>}</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> SWng_Menu;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.MouseAdapter;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.MouseEvent;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> javax.swing.filechooser.FileFilter;</span><br><span class="line"><span class="keyword">import</span> javax.swing.filechooser.FileNameExtensionFilter;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> 文件选择过滤器 </span>&#123;</span><br><span class="line">		JFrame f;</span><br><span class="line">		JLabel l;</span><br><span class="line">		<span class="keyword">public</span> 文件选择过滤器() &#123;</span><br><span class="line">			f=<span class="keyword">new</span> JFrame(<span class="string">&quot;文件选择过滤器&quot;</span>);</span><br><span class="line">			l=<span class="keyword">new</span> JLabel(<span class="string">&quot;&lt;双击打开照片&gt;&quot;</span>,SwingConstants.CENTER);</span><br><span class="line">			l.addMouseListener(<span class="keyword">new</span> MouseAdapter() &#123;</span><br><span class="line">				JFileChooser wjchoose;</span><br><span class="line">				&#123;</span><br><span class="line">					<span class="comment">//创建文件选择对话框</span></span><br><span class="line">					wjchoose=<span class="keyword">new</span> JFileChooser();</span><br><span class="line">					<span class="comment">//设置文件过滤器，只列出JPG或GIF格式的图片</span></span><br><span class="line">					FileFilter file=<span class="keyword">new</span> FileNameExtensionFilter(<span class="string">&quot;图像文件(JPG/GIF)&quot;</span>,<span class="string">&quot;JPG&quot;</span>,<span class="string">&quot;JPEG&quot;</span>,<span class="string">&quot;GIF&quot;</span>);</span><br><span class="line">					wjchoose.setFileFilter(file);</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">mouseClicked</span><span class="params">(MouseEvent e)</span> </span>&#123;</span><br><span class="line">					<span class="keyword">if</span>(e.getClickCount()==<span class="number">2</span>) &#123;<span class="comment">//点击次数</span></span><br><span class="line">						<span class="comment">//显示文件选择对话框</span></span><br><span class="line">						<span class="keyword">int</span> i=wjchoose.showOpenDialog(f);</span><br><span class="line">						<span class="comment">//判断用户点击的是否为“打开”按钮</span></span><br><span class="line">						<span class="keyword">if</span>(i==JFileChooser.APPROVE_OPTION) &#123;</span><br><span class="line">							<span class="comment">//获取选中的图片对象</span></span><br><span class="line">							File selectedFile=wjchoose.getSelectedFile();</span><br><span class="line">							ImageIcon img=<span class="keyword">new</span> ImageIcon(selectedFile.getAbsolutePath());  <span class="comment">//传入图片路径创建图片对象</span></span><br><span class="line">							l.setIcon(img);</span><br><span class="line">							f.setSize(img.getIconWidth(),img.getIconHeight());<span class="comment">//图片的大小显示窗口大小</span></span><br><span class="line">							l.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;);</span><br><span class="line">			</span><br><span class="line">			f.add(l);</span><br><span class="line">			f.setSize(<span class="number">300</span>,<span class="number">400</span>);</span><br><span class="line">			f.setLocation(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line">			f.setResizable(<span class="keyword">false</span>);</span><br><span class="line">			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line">			f.setVisible(<span class="keyword">true</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO 自动生成的方法存根</span></span><br><span class="line">		<span class="keyword">new</span> 文件选择过滤器();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/26/eef8a9afcfc97a8209e6d7323fdad6cd.png"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">小李博客</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://xiaoliblog.cn">https://xiaoliblog.cn</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://xiaoliblog.cn" target="_blank">小李博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaSE/">JavaSE</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.png" target="_blank"><img class="post-qr-code-img" src="/img/wechat.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.png" target="_blank"><img class="post-qr-code-img" src="/img/alipay.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/page/java01.html"><img class="prev-cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java👉IO流</div></div></a></div><div class="next-post pull-right"><a href="/page/java11.html"><img class="next-cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java👉线程、线程通信</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/page/java01.html" title="Java👉IO流"><img class="cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Java👉IO流</div></div></a></div><div><a href="/page/java06.html" title="Java👉泛型和枚举"><img class="cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Java👉泛型和枚举</div></div></a></div><div><a href="/page/java12.html" title="Java👉异常机制"><img class="cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Java👉异常机制</div></div></a></div><div><a href="/page/java03.html" title="Java👉JDBC"><img class="cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Java👉JDBC</div></div></a></div><div><a href="/page/java09.html" title="Java👉事件处理"><img class="cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Java👉事件处理</div></div></a></div><div><a href="/page/java11.html" title="Java👉线程、线程通信"><img class="cover" src="https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/12/30/0b4418a37c4d8fd66e1607c74793c6be.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-05</div><div class="title">Java👉线程、线程通信</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@6b5e7ef72be1c8973d94e5a9c49accbf775ad820/2021/02/01/c485da031fe0e464d04eaba8a66c4a8f.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">小李博客</div><div class="author-info__description">越努力，越幸运！</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">210</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">38</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">56</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xiaoliblog"><i class="fab fa-github"></i><span>博主的GitHub首页</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/xiaoliblog" target="_blank" title="Gitee"><i class="iconfont icon-gitee card_icon_gitee"></i></a><a class="social-icon" href="https://space.bilibili.com/390969485" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili card_icon_bilibili"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=2312057536&amp;site=CSDN&amp;menu=yes" target="_blank" title="QQ"><i class="iconfont icon-qq card_icon_qq"></i></a><a class="social-icon" href="https://github.com/xiaoliblog" target="_blank" title="GitHub"><i class="iconfont icon-git card_icon_git"></i></a><a class="social-icon" href="https://blog.csdn.net/qq_43266250?spm=1010.2135.3001.5113" target="_blank" title="CSDN"><i class="iconfont icon-csdn card_icon_csdn"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">正在考研备考中💦</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Swing%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1"><span class="toc-number">1.</span> <span class="toc-text">Swing程序设计</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8Swing%E7%BB%84%E4%BB%B6"><span class="toc-number">1.1.</span> <span class="toc-text">常用Swing组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E7%AA%97%E4%BD%93"><span class="toc-number">1.2.</span> <span class="toc-text">常用窗体</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.3.</span> <span class="toc-text">组件的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JLabel%E6%A0%87%E7%AD%BE"><span class="toc-number">1.3.1.</span> <span class="toc-text">JLabel标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%BE%E6%A0%87%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.3.2.</span> <span class="toc-text">图标的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8C%89%E9%92%AE%E7%BB%84%E4%BB%B6"><span class="toc-number">1.3.3.</span> <span class="toc-text">按钮组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8B%E6%8B%89%E6%A1%86%E3%80%81%E4%B8%8B%E6%8B%89%E5%88%97%E8%A1%A8%E5%88%97%E8%A1%A8%E5%92%8C%E6%BB%9A%E5%8A%A8%E6%9D%A1"><span class="toc-number">1.3.4.</span> <span class="toc-text">下拉框、下拉列表列表和滚动条</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8B%86%E5%88%86%E7%AA%97%E5%8F%A3"><span class="toc-number">1.3.5.</span> <span class="toc-text">拆分窗口</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86"><span class="toc-number">1.4.</span> <span class="toc-text">布局管理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%9D%E5%AF%B9%E5%B8%83%E5%B1%80"><span class="toc-number">1.4.1.</span> <span class="toc-text">绝对布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%81%E5%8A%A8%E5%B8%83%E5%B1%80"><span class="toc-number">1.4.2.</span> <span class="toc-text">流动布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BE%B9%E7%95%8C%E5%B8%83%E5%B1%80"><span class="toc-number">1.4.3.</span> <span class="toc-text">边界布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80"><span class="toc-number">1.4.4.</span> <span class="toc-text">网格布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%BC%8F%E5%B8%83%E5%B1%80"><span class="toc-number">1.4.5.</span> <span class="toc-text">盒式布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E5%B8%83%E5%B1%80"><span class="toc-number">1.4.6.</span> <span class="toc-text">卡片布局</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Swing%E9%AB%98%E7%BA%A7"><span class="toc-number">2.</span> <span class="toc-text">Swing高级</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#JTable%E8%A1%A8%E6%A0%BC"><span class="toc-number">2.1.</span> <span class="toc-text">JTable表格</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JTree%E6%A0%91"><span class="toc-number">2.2.</span> <span class="toc-text">JTree树</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JMenu%E8%8F%9C%E5%8D%95"><span class="toc-number">2.3.</span> <span class="toc-text">JMenu菜单</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8B%E6%8B%89%E8%8F%9C%E5%8D%95"><span class="toc-number">2.3.1.</span> <span class="toc-text">下拉菜单</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%B9%E5%87%BA%E8%8F%9C%E5%8D%95"><span class="toc-number">2.3.2.</span> <span class="toc-text">弹出菜单</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E5%88%B6%E8%8F%9C%E5%8D%95"><span class="toc-number">2.3.3.</span> <span class="toc-text">定制菜单</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%88%86%E9%9A%94%E7%BA%BF"><span class="toc-number">2.3.3.1.</span> <span class="toc-text">使用分隔线</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8-%E7%A6%81%E7%94%A8"><span class="toc-number">2.3.3.2.</span> <span class="toc-text">启动&#x2F;禁用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="toc-number">2.3.3.3.</span> <span class="toc-text">设置快捷键</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AE%E5%8A%A0%E9%80%9F%E5%99%A8"><span class="toc-number">2.3.3.4.</span> <span class="toc-text">设置加速器</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AC%94%E8%AE%B0%E6%9C%AC%E5%AE%9E%E4%BE%8B"><span class="toc-number">2.3.4.</span> <span class="toc-text">笔记本实例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%8A%A8%E5%BC%B9%E7%AA%97"><span class="toc-number">2.4.</span> <span class="toc-text">自动弹窗</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.5.</span> <span class="toc-text">文件选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E9%80%89%E6%8B%A9%E5%AF%B9%E8%AF%9D%E6%A1%86"><span class="toc-number">2.5.1.</span> <span class="toc-text">文件选择对话框</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">2.5.2.</span> <span class="toc-text">文件过滤器</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/page/project01.html" title="前端实例🥳响应式网站首页"><img src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@c19917500ab083c77c7613263ba7ee74d5a08ae6/2021/04/30/469f30b141d73fa0fc4c962662d5813f.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="前端实例🥳响应式网站首页"/></a><div class="content"><a class="title" href="/page/project01.html" title="前端实例🥳响应式网站首页">前端实例🥳响应式网站首页</a><time datetime="2021-04-30T11:50:53.094Z" title="发表于 2021-04-30 19:50:53">2021-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/page/WeChatpay.html" title="微信支付对接"><img src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@f2f670b92ea149650ffa7834354fc90284f2f44a/2021/04/29/5bdc9b381a06193d27cf2fb7c2fb608a.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="微信支付对接"/></a><div class="content"><a class="title" href="/page/WeChatpay.html" title="微信支付对接">微信支付对接</a><time datetime="2021-04-29T12:20:48.070Z" title="发表于 2021-04-29 20:20:48">2021-04-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/page/Alipay.html" title="支付宝支付对接"><img src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@61a9b6a6e09e4bda38bb08e3104b717885beaee5/2021/04/29/c3fa51f9cf14e90d9e5a7aa8814dd041.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="支付宝支付对接"/></a><div class="content"><a class="title" href="/page/Alipay.html" title="支付宝支付对接">支付宝支付对接</a><time datetime="2021-04-27T16:00:00.000Z" title="发表于 2021-04-28 00:00:00">2021-04-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/page/Vuejs11.html" title="Vue.js前端框架🎯Pagination+PageHelper实现分页"><img src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@61bf4245f6d84d2d0f66d163b89c916788cc1845/2021/04/13/ec6b232f8fe5a840e4bd8c3eabcf49b2.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="Vue.js前端框架🎯Pagination+PageHelper实现分页"/></a><div class="content"><a class="title" href="/page/Vuejs11.html" title="Vue.js前端框架🎯Pagination+PageHelper实现分页">Vue.js前端框架🎯Pagination+PageHelper实现分页</a><time datetime="2021-04-26T14:48:39.701Z" title="发表于 2021-04-26 22:48:39">2021-04-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/page/Vuejs10.html" title="Vue.js前端框架🎯Vue-Element-admin模版"><img src="https://cdn.jsdelivr.net/gh/xiaoliblog/image@61bf4245f6d84d2d0f66d163b89c916788cc1845/2021/04/13/ec6b232f8fe5a840e4bd8c3eabcf49b2.png" onerror="this.onerror=null;this.src='https://cdn.jsdelivr.net/gh/lzyblog/image@main/2020/11/19/bd16b394f7359083b1f6072a67e3f968.png'" alt="Vue.js前端框架🎯Vue-Element-admin模版"/></a><div class="content"><a class="title" href="/page/Vuejs10.html" title="Vue.js前端框架🎯Vue-Element-admin模版">Vue.js前端框架🎯Vue-Element-admin模版</a><time datetime="2021-04-19T16:00:00.000Z" title="发表于 2021-04-20 00:00:00">2021-04-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 小李博客</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">🎯Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">🎉Butterfly</a></div><div class="footer_custom_text"><span><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a><a style="margin-inline:5px" target="_blank" href="https://vercel.com/ "><img src="https://img.shields.io/badge/Hosted-Vercel-brightgreen?style=flat&logo=Vercel" title="本站采用双线部署，默认线路托管于Vercel"></a><a style="margin-inline:5px" target="_blank" href="https://coding.net/ "><img src="https://img.shields.io/badge/Hosted-Coding-0cedbe?style=flat&logo=Codio" title="本站采用双线部署，联通线路托管于Coding"></a><a style="margin-inline:5px" target="_blank" href="https://github.com/"><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&logo=GitHub" title="本站项目由Gtihub托管"></a><a style="margin-inline:5px" target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris" title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可"></a></span></div><div class="icp"><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/"><img class="icp-icon" src="/img/icp.png" alt="ICP"/><span>湘ICP备2021002541号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="rightside.chat_btn"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><div class="search-dialog__title" id="algolia-search-title">Algolia</div><div id="algolia-input-panel"><div id="algolia-search-input"></div></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-stats"></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script><script src="/js/main.js"></script><script defer src="/js/tw_cn.js"></script><script defer src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module" defer></script><script defer src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script defer src="/js/search/algolia.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    loader: {
      source: {
        '[tex]/amsCd': '[tex]/amscd'
      }
    },
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        addClass: [200,() => {
          document.querySelectorAll('mjx-container:not([display=\'true\']').forEach( node => {
            const target = node.parentNode
            if (!target.classList.contains('has-jax')) {
              target.classList.add('mathjax-overflow')
            }
          })
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      false && mermaid.init()
    })
  }
}</script><script>(()=>{
  const $countDom = document.getElementById('twikoo-count')
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: 'xiaoliblog-8gj3j5045d5b0896',
      region: ''
    }, null))
  }

  const getCount = () => {
    twikoo.getCommentsCount({
      envId: 'xiaoliblog-8gj3j5045d5b0896',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      $countDom.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const loadTwikoo = (bool = false) => {
    if (typeof twikoo === 'object') {
      init()
      bool && $countDom && setTimeout(getCount,0)
    } else {
      getScript('https://cdn.jsdelivr.net/npm/twikoo@1.3.0/dist/twikoo.all.min.js').then(()=> {
        init()
        bool && $countDom && setTimeout(getCount,0)
      })
    }
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo(true)
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script defer src="//lib.baomitu.com/jquery/3.5.1/jquery.min.js"></script><script defer src="https://myhkw.cn/api/player/160561664166" id="myhk" key="160561664166" m="1"></script><div><canvas id="snow" style="position:fixed;top:0;left:0;width:100%;height:100%;z-index:99999;pointer-events:none"></canvas></div><script>const notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));</script><scrip async type="text/javascript" src="https://cdn.jsdelivr.net/gh/Candinya/Kratos-Rebirth@latest/source/js/snow.min.js"></scrip><scrip defer src="https://cdn.jsdelivr.net/npm/hexo-theme-volantis@latest/source/js/issues.min.js"></scrip><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script>var gitcalendar = new Vue({
  el: '#gitcalendar',
  data: {
    simplemode: true, 
    user: 'xiaoliblog',
    fixed: 'fixed',
    px: 'px',
    x: '',
    y: '',
    span1: '',
    span2: '',
    month: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
    monthchange: [],
    oneyearbeforeday: '',
    thisday: '',
    amonthago: '',
    aweekago: '',
    weekdatacore: 0,
    datacore: 0,
    total: 0,
    datadate: '',
    data: [],
    positionplusdata: [],
    firstweek: [],
    lastweek: [],
    beforeweek: [],
    thisweekdatacore: 0,
    mounthbeforeday: 0,
    mounthfirstindex: 0,
    crispedges: 'crispedges',
    thisdayindex: 0,
    amonthagoindex: 0,
    amonthagoweek: [],
    firstdate: [],
    first2date: [],
    montharrbefore: [],
    monthindex: 0,
    color: ['#ebedf0', '#f1f8ff', '#dbedff', '#c8e1ff', '#79b8ff', '#2188ff', '#0366d6', '#005cc5', '#044289', '#032f62', '#05264c']
  },
  methods: {
    selectStyle(data, event) {
      document.querySelector('.angle-wrapper').style.display = 'block'
      this.span1 = data.date;
      this.span2 = data.count;
      this.x = event.clientX - 100;
      this.y = event.clientY - 60
    },
    outStyle() {
      document.querySelector('.angle-wrapper').style.display = 'none'
    },
    thiscolor(x) {
      if (x === 0) {
        let i = parseInt(x / 2);
        return this.color[0]
      } else if (x < 2) {
        return this.color[1]
      } else if (x < 20) {
        let i = parseInt(x / 2);
        return this.color[i]
      } else {
        return this.color[9]
      }
    },
  }
});
var apiurl = 'python-github-calendar-api-ruby.vercel.app' ? 'https://python-github-calendar-api-ruby.vercel.app/api?' : 'https://githubapi.ryanchristian.dev/user/'
var githubapiurl = apiurl + gitcalendar.user;
//canvas绘图
function responsiveChart() {
  let c = document.getElementById("gitcanvas");
  if (c) {
    let cmessage = document.getElementById("gitmessage");
    let ctx = c.getContext("2d");
    c.width = document.getElementById("gitcalendarcanvasbox").offsetWidth;
    let linemaxwitdh = 0.96 * c.width / gitcalendar.data.length;
    c.height = 9 * linemaxwitdh;
    let lineminwitdh = 0.8 * linemaxwitdh;
    let setposition = {
      x: 0.02 * c.width,
      y: 0.025 * c.width
    };
    for (let week in gitcalendar.data) {
      weekdata = gitcalendar.data[week];
      for (let day in weekdata) {
        let dataitem = {
          date: "",
          count: "",
          x: 0,
          y: 0
        };
        gitcalendar.positionplusdata.push(dataitem);
        ctx.fillStyle = gitcalendar.thiscolor(weekdata[day].count);
        setposition.y = Math.round(setposition.y * 100) / 100;
        dataitem.date = weekdata[day].date;
        dataitem.count = weekdata[day].count;
        dataitem.x = setposition.x;
        dataitem.y = setposition.y;
        ctx.fillRect(setposition.x, setposition.y, lineminwitdh, lineminwitdh);
        setposition.y = setposition.y + linemaxwitdh
      };
      setposition.y = 0.025 * c.width;
      setposition.x = setposition.x + linemaxwitdh
    };
    ctx.font = "600  Arial";
    ctx.fillStyle = '#aaa';
    ctx.fillText("日", 0, 1.9 * linemaxwitdh);
    ctx.fillText("二", 0, 3.9 * linemaxwitdh);
    ctx.fillText("四", 0, 5.9 * linemaxwitdh);
    ctx.fillText("六", 0, 7.9 * linemaxwitdh);
    let monthindexlist = c.width / 24;
    for (let index in gitcalendar.monthchange) {
      ctx.fillText(gitcalendar.monthchange[index], monthindexlist, 0.7 * linemaxwitdh);
      monthindexlist = monthindexlist + c.width / 12
    };
    cmessage.onmousemove = function(event) {
      document.querySelector('.angle-wrapper').style.display = 'none'
    };
    c.onmousemove = function(event) {
      document.querySelector('.angle-wrapper').style.display = 'none'
      getMousePos(c, event);
    };

    function getMousePos(canvas, event) {
      var rect = canvas.getBoundingClientRect();
      var x = event.clientX - rect.left * (canvas.width / rect.width);
      var y = event.clientY - rect.top * (canvas.height / rect.height);
      //console.log("x:"+x+",y:"+y);
      for (let item of gitcalendar.positionplusdata) {
        let lenthx = x - item.x;
        let lenthy = y - item.y;
        //console.log(lenthx,lenthy);
        if (0 < lenthx && lenthx < lineminwitdh) {
          if (0 < lenthy && lenthy < lineminwitdh) {
            //console.log(item.date,item.count)
            document.querySelector('.angle-wrapper').style.display = 'block'
            gitcalendar.span1 = item.date;
            gitcalendar.span2 = item.count;
            gitcalendar.x = event.clientX - 100;
            gitcalendar.y = event.clientY - 60
          }
        }
        //if(0< x - item.x <lineminwitdh&&0< y - item.y <lineminwitdh){
        //console.log(item.count,item.date);
        //}
      }
    }
  }
}
//数据统计算法
function addlastmonth() {
  if (gitcalendar.thisdayindex === 0) {
    thisweekcore(52);
    thisweekcore(51);
    thisweekcore(50);
    thisweekcore(49);
    thisweekcore(48);
    gitcalendar.thisweekdatacore += gitcalendar.firstdate[6].count;
    gitcalendar.amonthago = gitcalendar.firstdate[6].date
  } else {
    thisweekcore(52);
    thisweekcore(51);
    thisweekcore(50);
    thisweekcore(49);
    thisweek2core();
    gitcalendar.amonthago = gitcalendar.first2date[gitcalendar.thisdayindex - 1].date
  }
};

function thisweek2core() {
  for (let i = gitcalendar.thisdayindex - 1; i < gitcalendar.first2date.length; i++) {
    gitcalendar.thisweekdatacore += gitcalendar.first2date[i].count
  }
};

function thisweekcore(index) {
  for (let item of gitcalendar.data[index]) {
    gitcalendar.thisweekdatacore += item.count
  }
};

function addlastweek() {
  for (let item of gitcalendar.lastweek) {
    gitcalendar.weekdatacore += item.count
  }
};

function addbeforeweek() {
  for (let i = gitcalendar.thisdayindex; i < gitcalendar.beforeweek.length; i++) {
    gitcalendar.weekdatacore += gitcalendar.beforeweek[i].count
  }
};

function addweek(data) {
  if (gitcalendar.thisdayindex === 6) {
    gitcalendar.aweekago = gitcalendar.lastweek[0].date;
    addlastweek()
  } else {
    lastweek = data.contributions[51];
    gitcalendar.aweekago = lastweek[gitcalendar.thisdayindex + 1].date;
    addlastweek();
    addbeforeweek()
  }
}

fetch(githubapiurl)
  .then(data => data.json())
  .then(data => {
    gitcalendar.data = data.contributions;
    gitcalendar.total = data.total;
    gitcalendar.first2date = gitcalendar.data[48];
    gitcalendar.firstdate = gitcalendar.data[47];
    gitcalendar.firstweek = data.contributions[0];
    gitcalendar.lastweek = data.contributions[52];
    gitcalendar.beforeweek = data.contributions[51];
    gitcalendar.thisdayindex = gitcalendar.lastweek.length - 1;
    gitcalendar.thisday = gitcalendar.lastweek[gitcalendar.thisdayindex].date;
    gitcalendar.oneyearbeforeday = gitcalendar.firstweek[0].date;
    gitcalendar.monthindex = gitcalendar.thisday.substring(5, 7) * 1;
    gitcalendar.montharrbefore = gitcalendar.month.splice(gitcalendar.monthindex, 12 - gitcalendar.monthindex);
    gitcalendar.monthchange = gitcalendar.montharrbefore.concat(gitcalendar.month);
    addweek(data);
    addlastmonth();
    responsiveChart();
  })
  .catch(function(error) {
    console.log(error);
  });

//手机版更换为svg绘制
if (document.getElementById("gitcalendarcanvasbox").offsetWidth < 500) {
  gitcalendar.simplemode = false
}

//当改变窗口大小时重新绘制canvas
window.onresize = function() {
  if (gitcalendar.simplemode) responsiveChart()
}

//解决滚动滑轮时出现的标签显示
window.onscroll = function() {
  if (document.querySelector('.angle-wrapper')) {
    document.querySelector('.angle-wrapper').style.display = 'none'
  }
};</script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body></html>